DynamoDB - How to create map and add attribute to it in one update DynamoDB - How to create map and add attribute to it in one update node.js node.js

DynamoDB - How to create map and add attribute to it in one update


You can amortize the cost of doing two seperate UpdateItem calls, one to create #A, and the other to add #B to #A by adding #B to #A with a conditional update.

UpdateExpression: SET #A.#B = :valueOfBConditionExpression: attribute_exists(#A)

If you add many entries to #A, then you only create #A once, and as the number of entries in #A increases, the amortized time to create #A approaches zero. If you catch a ConditionalCheckFailedException, that is when you would create the map with #B already in it and call UpdateItem:

UpdateExpression: SET #A = :valueOfMapWithBInItConditionExpression: attribute_not_exists(#A)