47

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 ?

Hoang Nguyen Ba
  • 531
  • 1
  • 5
  • 7

1 Answers1

116

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",
notionquest
  • 37,595
  • 6
  • 111
  • 105
  • 10
    Don't know why this isn't in the official docs examples tbh. TY https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.ADD – Blundell Jun 24 '20 at 06:41