DynamoDB:仅当它已经存在时才更新项目

默认情况下,如果具有指定索引的对象不存在,DynamoDB 将创建一个新条目.有没有办法阻止这种情况发生?我可以在更新之前在表中查询密钥,但最好在一个请求中完成所有操作.

By default DynamoDB will make a new entry if an object with the specified index doesn't exist. Is there a way to stop this from occurring? I could just query the table for the key before updating, but it would be nice to do it all in one request.

推荐答案

使用条件表达式 id = :id 其中 id 是属性名(或主键名您的情况)和 :id 是值(您要更新的项目的键).

Use conditional expression id = :id where id is the attribute name (or primary key name in your case) and :id is the value (key of the item you want to update).

条件表达式总是在任何写入之前进行评估.如果该表达式的计算结果不是 true(并且如果该键不存在或不同,则不会),它不会更新或放置新项目.

Conditional expression is always evaluated before any writing. If that expression doesn't evaluate to true (and it doesn't if that key doesn't exist or is different), it doesn't update or put a new item.

相关文章