所以这应该很简单......
如果存在,我想将字符串附加到 DynamoDB 中的 StringSet,如果不存在,则创建 StringSet 属性并设置该值。如果我们可以在创建时用一个空数组初始化 StringSet,那就没问题了,但可惜我们不能。
这是我到目前为止所拥有的:
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);
});
我也试过...
UpdateExpression: 'SET socialAccounts = list_append(socialAccounts, :socialAccountId)',
ExpressionAttributeValues: {
':socialAccountId': {
S: socialAccountId
}
},
和...
UpdateExpression: 'ADD socialAccounts = :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
S: socialAccountId
}
},
和...
UpdateExpression: 'SET socialAccounts = [:socialAccountId]',
ExpressionAttributeValues: {
':socialAccountId': socialAccountId
},
和...
UpdateExpression: 'ADD socialAccounts = :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': socialAccountId
},
在上述所有其他变体中。我傻吗? DynamoDB 是否不能对数组类型字段进行简单的写入/更新?在尝试添加或设置该字段之前,我真的必须先查找该项目以查看它是否具有该字段,因为我无法使用空数组实例化该字段吗?
最佳答案
ADD 操作处理创建/更新逻辑,但仅支持数字和集合。您正在尝试添加字符串类型“S”。您需要将此字符串包装在一个数组中并将其作为字符串集“SS”传递。您也不需要等号。
您的 UpdateExpression 和 ExpressionAttributeValues 应如下所示:
UpdateExpression: 'ADD socialAccounts :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
'SS': [socialAccountId]
}
},
有关更新项目的更多信息,请访问 here
关于amazon-web-services - 如果 StringSet 不存在,则附加到或创建它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39009687/