- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用户表,有一个名为 friend 的属性,它将是一组用户所有 friend 的 id。
最初,我尝试在创建用户时将friends 属性实例化为空集,但我收到一个错误,提示您不能拥有空属性。
因此,如果有人还没有 friend ,我能找到的唯一解决方案是读取用户的属性,如果它不存在,则将属性设置为与他们添加的 friend 一起设置的 [新] 集。如果确实存在,则只需使用 ADD 执行更新,将新 friend 添加到集合中。
我不想为此两次调用 AWS。
有没有办法在集合不存在的情况下创建它,如果存在,则将其添加到其中 - 只需 1 次调用?
最佳答案
对于 SET 数据类型(来自 DynamoDB API Reference ):
ADD - If the attribute does not already exist, then the attribute and its values are added to the item. If the attribute does exist,then the behavior of ADD depends on the data type of the attribute:
If the existing data type is a set, and if the Value is also a set,then the Value is added to the existing set. (This is a set operation,not mathematical addition.) For example, if the attribute value wasthe set [1,2], and the ADD action specified [3], then the finalattribute value would be [1,2,3]. An error occurs if an Add action isspecified for a set attribute and the attribute type specified doesnot match the existing set type. Both sets must have the sameprimitive data type. For example, if the existing data type is a setof strings, the Value must also be a set of strings. The same holdstrue for number sets and binary sets.
country
表中不存在属性。
updateItem
创建了新属性
country
提供的值(IN,UK)。
var params = {
TableName : "Movies",
Key : {
"yearkey" : 2014,
"title" : "The Big New Movie 2"
},
UpdateExpression : "ADD country :countries",
ExpressionAttributeValues: {
':countries': docClient.createSet(["IN", "UK"])
},
ReturnValues : "UPDATED_NEW"
};
第二次更新:-
updateItem
添加了新值“US”并忽略了现有值“IN”。
var params = {
TableName : "Movies",
Key : {
"yearkey" : 2014,
"title" : "The Big New Movie 2"
},
UpdateExpression : "ADD country :countries",
ExpressionAttributeValues: {
':countries': docClient.createSet(["IN", "US"])
},
ReturnValues : "UPDATED_NEW"
};
关于amazon-web-services - AWS DynamoDB - 如果设置存在,如何在 1 次调用中实现 : Add value to set, - 或者使用值实例化设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42103263/
我是一名优秀的程序员,十分优秀!