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

查看:32
本文介绍了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天全站免登陆