dynamo icon indicating copy to clipboard operation
dynamo copied to clipboard

Incrementing a value

Open maxisme opened this issue 3 years ago • 1 comments

Hello,

How would you recommend I do something like this with this library:

dynamoDB.updateItem({
  TableName: "Users",
  Key: { "UserId": { S: "c6af9ac6-7b61" } },
  ExpressionAttributeValues: { ":inc": {N: "1"} },
  UpdateExpression: "ADD loginCount :inc"
})

I am currently fetching the row getting the value then adding 1 to it but I am worried about race conditions.

maxisme avatar Sep 22 '21 13:09 maxisme

That is supported. Just do:

session.Table("tableName").Update("hashKeyName", hashKeyValue).Add("fieldToUpdate", 1).Run()

Replace Run with OldValue if you want to retrieve the old value

shimonmagal avatar Nov 22 '21 21:11 shimonmagal