DynamoDB-如何在一次更新中创建地图并为其添加属性 [英] DynamoDB - How to create map and add attribute to it in one update

查看:59
本文介绍了DynamoDB-如何在一次更新中创建地图并为其添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不存在,我想创建一个新地图,然后向该地图添加一个属性.像这样:

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.

是否可以在一次更新中完成?

更新

另一个用例是创建包含其他地图的地图.目前,我想到的唯一方法是创建3个单独的更新调用(必要时创建地图),然后再进行另一个添加属性的更新调用:

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: {} } },
}

必须有更好的方法.

推荐答案

您可以分摊两次调用UpdateItem的成本,一次调用创建#A,另一次通过将#B添加到#A中来将#B添加到#A中.#A有条件的更新.

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)

如果将许多条目添加到#A中,则只需创建一次#A,并且随着#A中条目数量的增加,创建#A的摊销时间将接近零.如果您发现ConditionalCheckFailedException,那就是在其中已创建#B的地图并调用UpdateItem的时候:

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)

这篇关于DynamoDB-如何在一次更新中创建地图并为其添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆