Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB UpdateItem calculation with numbers?

I would like my UpdateItem function to add the new value to the previous one but I can't figure out how to use numbers for the calculation and access the current value to be added to the new one.

Here is my working function, what I want to do is included in comment. How can I do it?

{
    "TableName": "Votes",
    "Key": {
        "votesId": {
            "S": "$input.path('$.votesId')"
        }
    },
    "UpdateExpression": "set buy =  :val1",
    "ExpressionAttributeValues" : {
        ":val1": { 
            "N": "$input.path('$.buy')" 
            //Would like: "N": "buy + $input.path('$.buy')"
        }
    },
    "ReturnValues": "ALL_NEW"
}

Here is how I test it:

{
    "votesId":1,
    "down":0,
    "up":0,
    "hold":0,
    "buy":0,
    "sell":0
}
like image 637
Wizzardzz Avatar asked Mar 18 '26 02:03

Wizzardzz


1 Answers

You can use UpdateExpression to indicate which attributes are to be updated and what actions to perform.

Specifically in your case, UpdateExpression supports the following:

SET myNum = myNum + :val
like image 111
jarmod Avatar answered Mar 19 '26 17:03

jarmod