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

最后都变了- 提交于 2020-12-02 10:42:35

问题


I want to create a new map if it doesn't exist and then add an attribute to that map. Something like this:

SET #A = if_not_exists(#A, :emptyMap), #A.#B = :somevalue

However doing the above gives me the error saying Two document paths overlap with each other

The only other thing I am thinking to do is do TWO updates, one to create any empty maps and then another to set attributes.

Is there a way to do it in a single update ?

Update

Another use case is creating maps that contain other maps. Currently the only way I can think of to create the following is 3 separate update calls to create the maps if necessary and then another update call to add attributes:

{
  Entities: { A: { B: {} } },
}

There must be a better way.


回答1:


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 = :valueOfB
ConditionExpression: 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 = :valueOfMapWithBInIt
ConditionExpression: attribute_not_exists(#A)


来源:https://stackoverflow.com/questions/43131028/dynamodb-how-to-create-map-and-add-attribute-to-it-in-one-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!