gruuuvy
gruuuvy

Reputation: 2129

How to update a DynamoDB Document (JSON) atomically?

I have an attribute in my table that is of type Document (JSON) where the value is a count:

{ "item1" : "5", "item2" : "7" }

Is there a way for the DynamoDB Document API to increment the values of the map atomically? My application will have several hosts incrementing the values, so simply using puts will not work as they will overwrite each other.

I know for integer attributes, we can have atomic counting with set #count = #count + :countVal in the update expression. Is there something similar for working with Documents?

Also, I noticed that there is no way for DynamoDBMapper to make atomic counter updates, so I do have to use the lower level Document API, right?

Upvotes: 0

Views: 1091

Answers (1)

xaksis
xaksis

Reputation: 458

If I understand correctly, you have one of your attributes as a Map, with key value pairs in your table. Dynamodb supports document paths in expressions. So you can do

doc.item1 = doc.item1 + :countVal

You are correct in that dynamodbmapper doesn't support atomic counter updates. It seems to be pretty common to use a combination of dynamodbmapper and lower level API though.

Upvotes: 2

Related Questions