Maor Cohen
Maor Cohen

Reputation: 956

Update dynamoDB key that contains map

I'm using:

http://awspilot.github.io/dynamodb-oop/#query

(aws-sdk wrapper)

I have an object that is stored in DB. I want to update a value that it's a map.

This is my table:

userId, myMap

123     {'abc': '123'}

And this is the code:

var tableName = 'myTable';
var userIdKey = 'userId';

function updateMyMap(userId, dic) {

    DynamoDB
    .table(tableName)
    .where(userIdKey).eq(userId)
    .return(DynamoDB.ALL_OLD)
    .insert_or_update({
        myMap: dic
    }, function( err, data ) {
        console.log( err, data )
    })
}

First example:

When I call updateMyMap:

updateMyMap('123', {'abc':'456', 'def':'555'});

I want the table to be:

userId, myMap

123     {'abc': '456', 'def':'555'}

Second example:

When I call updateMyMap:

updateMyMap('123', {'ghi':'222'});

I want the table to be:

userId, myMap

123     {'abc': '456', 'def':'555', 'ghi':'222'}

Any help appreciated!

Upvotes: 0

Views: 626

Answers (1)

Alexander Patrikalakis
Alexander Patrikalakis

Reputation: 5205

Use an update expression in an UpdateItem call:

SET myMap.#key = :value

The other parameters would be:

ConditionExpression: attribute_exists(myMap)
ExpressionAttributeNames: { "#key": "ghi" }
ExpressionAttributeValues: { ":value": 222 }

Upvotes: 2

Related Questions