[英]Append to or create StringSet if it doesn't exist
So this should be simple... 所以这应该很简单......
I want to append a string to a StringSet in a DynamoDB if it exists, or create the StringSet property if it doesn't and set the value. 我想将一个字符串附加到DynamoDB中的StringSet(如果存在),或者创建StringSet属性(如果不存在)并设置该值。 If we could initialize the StringSet on creation with an empty array, it would be fine, but alas we can not.
如果我们可以在创建时使用空数组初始化StringSet,那就没问题,但是我们做不到。
Here's what I have so far: 这是我到目前为止所拥有的:
const companiesTable = 'companies';
dynamodb.updateItem({
TableName: companiesTable,
Key: {
id: {
S: company.id
}
},
UpdateExpression: 'ADD socialAccounts = list_append(socialAccount, :socialAccountId)',
ExpressionAttributeValues: {
':socialAccountId': {
'S': [socialAccountId]
}
},
ReturnValues: "ALL_NEW"
}, function(err, companyData) {
if (err) return cb({ error: true, message: err });
const response = {
error: false,
message: 'Social account created',
socialAccountData
};
cb(response);
});
I've also tried... 我也试过......
UpdateExpression: 'SET socialAccounts = list_append(socialAccounts, :socialAccountId)',
ExpressionAttributeValues: {
':socialAccountId': {
S: socialAccountId
}
},
and... 和...
UpdateExpression: 'ADD socialAccounts = :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
S: socialAccountId
}
},
and... 和...
UpdateExpression: 'SET socialAccounts = [:socialAccountId]',
ExpressionAttributeValues: {
':socialAccountId': socialAccountId
},
and... 和...
UpdateExpression: 'ADD socialAccounts = :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': socialAccountId
},
Among about every other variation of the above. 在上述的每一个变化中。 Am I dumb?
我笨吗? Is DynamoDB not capable of simple write/updates to an array type field?
DynamoDB不能简单地写入/更新数组类型字段吗? Do I REALLY have to lookup the item first to see if it has that field before I try to either add or set that field, because I can't instantiate that field with an empty array?
在尝试添加或设置该字段之前,我是否真的必须首先查找该项以查看它是否具有该字段,因为我无法使用空数组实例化该字段?
The ADD action handles the create/update logic, but only supports numbers and sets. ADD操作处理创建/更新逻辑,但仅支持数字和集。 You are trying to add a string type 'S'.
您正在尝试添加字符串类型“S”。 You need to wrap this string in an array and pass it as a string set 'SS'.
您需要将此字符串包装在一个数组中,并将其作为字符串集'SS'传递。 You also don't need the equals sign.
你也不需要等号。
Your UpdateExpression and ExpressionAttributeValues should look like this: 您的UpdateExpression和ExpressionAttributeValues应如下所示:
UpdateExpression: 'ADD socialAccounts :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
'SS': [socialAccountId]
}
},
More information about updating items can be found here 有关更新项目的更多信息,请访问此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.