是否可以在DynamoDB中进行条件放置或更新? [英] Is it possible to do a conditional put or update in DynamoDB?

查看:219
本文介绍了是否可以在DynamoDB中进行条件放置或更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我将具有以下结构的记录存储在DynamoDB中:

Let's say that I am storing records with following structure in DynamoDB:

{    
    "id": "57cf5b43-f9ec-4796-9de6-6a50f556cfd8",
    "created_at": "2015-09-18T13:27:00+12:00",
    "count": 3
}

现在,是否可以在一个请求中实现以下目标:

Now, is it possible to achieve the following in one request:


  • 如果具有给定 id 的记录不存在,则应使用count = 1
  • $ b创建$ b
  • 如果该 id 的记录存在,则计数器正在更新。

  • if the record with given id doesn't exist it should be created with count = 1
  • if the record for that id exists the counter is being updated.

当前,我正在查询查询记录是否存在,并根据结果执行 put update 。最好将其折叠为一个操作。

Currently I'm doing a query to check if the record exist and depending on the result I do a put or an update. It would be nice to fold that into a single operation.

推荐答案

您可以使用 UpdateItem API和 UpdateExpression 。由于 count 在此处将是 Number 类型,因此可以使用 SET ADD 表达式:

You can do this with UpdateItem API and the UpdateExpression because of your use case. Since count will be a Number type here, you can use the SET or ADD expressions:

ADD 告诉您可以将其用于 Number 类型(强调我的类型):

The documentation for ADD tells you that you can use it for Number types (emphasis mine):



  • ADD-如果属性尚不存在,则将指定的值添加到项目中。如果该属性确实存在,则ADD的行为取决于该属性的数据类型:

  • ADD - Adds the specified value to the item, if the attribute does not already exist. If the attribute does exist, then the behavior of ADD depends on the data type of the attribute:


  • 如果现有属性为数字,并且如果 Value 也是一个数字,则 Value 会在数学上添加到现有属性中。如果 Value 为负数,则从现有属性中减去它。

  • If the existing attribute is a number, and if Value is also a number, then Value is mathematically added to the existing attribute. If Value is a negative number, then it is subtracted from the existing attribute.

如果您使用ADD来增加或减少更新前不存在的项目的数值,DynamoDB使用0作为初始值。类似地,如果对现有项目使用ADD来增加或减少属性在更新之前不存在的值,DynamoDB使用0作为初始值。例如,假设您要更新的商品没有名为itemcount的属性,但是您还是决定将数字3添加到该属性中。 DynamoDB将创建itemcount属性,将其初始值设置为0,最后将其添加3。结果将是项目中的新itemcount属性,其值为3。

If you use ADD to increment or decrement a number value for an item that doesn't exist before the update, DynamoDB uses 0 as the initial value. Similarly, if you use ADD for an existing item to increment or decrement an attribute value that doesn't exist before the update, DynamoDB uses 0 as the initial value. For example, suppose that the item you want to update doesn't have an attribute named itemcount, but you decide to ADD the number 3 to this attribute anyway. DynamoDB will create the itemcount attribute, set its initial value to 0, and finally add 3 to it. The result will be a new itemcount attribute in the item, with a value of 3.

例如,您可以将 UpdateExpression 设置为 ADD #c:n ,其中:n 具有 < Number 类型的code> ExpressionAttributeValue 1 值,并且 #c 具有 ExpressionAttributeName 代替 count 。您需要为 count 使用占位符,因为它是保留字

For your example, you could have your UpdateExpression be ADD #c :n, where :n has an ExpressionAttributeValue of the Number type, 1 is the value, and #c has the ExpressionAttributeName substitution for count. You need to use a placeholder for count because it is a reserved word.

请参见 使用更新表达式修改项目和属性

这篇关于是否可以在DynamoDB中进行条件放置或更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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