Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB update Item multi action

I am trying to update an item in DynamoDB table:

var params = {
        TableName: 'User',
    Key: {
        id: 'b6cc8100-74e4-11e6-8e52-bbcb90cbdd26',
    },
    UpdateExpression: 'ADD past_visits :inc, past_chats :inc',
    ExpressionAttributeValues: {
        ':inc': 1
    },
        ReturnValues: 'ALL_NEW'
    };
    docClient.update(params, function(err, data) {
        if (err) ppJson(err); // an error occurred
        else ppJson(data); // successful response
    });        

It's working. But I want to set some more value (reset_time = :value') like this:

var params = {
        TableName: 'User',
    Key: {
        id: 'b6cc8100-74e4-11e6-8e52-bbcb90cbdd26',
    },
    UpdateExpression: 'ADD past_visits :inc, past_chats :inc, SET reset_time = :value',
    ExpressionAttributeValues: {
        ':inc': 1,
        ':value': 0
    },
        ReturnValues: 'ALL_NEW'
    };
    docClient.update(params, function(err, data) {
        if (err) ppJson(err); // an error occurred
        else ppJson(data); // successful response
    });

Can DynamoDb support multi action in one query ?

like image 352
Hoang Nguyen Ba Avatar asked Sep 08 '16 03:09

Hoang Nguyen Ba


People also ask

How do you update multiple items in a DynamoDB table?

The only way to update multiple items at the same time is use TransactionWrite operation provided by DynamoDB. But it comes with a limitation (25 at most for example). So keep in mind with that, you probable should do some limitation in your application as well.

Can we update multiple items in DynamoDB?

(If you want to modify multiple items, you must use multiple operations.) With the DynamoDB API, you use the UpdateItem action to modify a single item. You must specify the Key attributes of the item to be modified and an UpdateExpression to specify attribute values.

How do I update a DynamoDB item?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

Can we update PK in DynamoDB?

You cannot update the primary key attributes using UpdateItem. Instead, delete the item and use PutItem to create a new item with new attributes.


1 Answers

Please change the update expression as mentioned below. It should work.

There is no comma between second ":inc" and SET.

UpdateExpression : "ADD past_visits :inc, past_chats :inc  SET reset_time = :value", 
like image 164
notionquest Avatar answered Oct 04 '22 08:10

notionquest