如果不存在则追加或创建 StringSet
Append to or create StringSet if it doesn't exist
所以这应该很简单...
我想将字符串附加到 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 不能对数组类型字段进行简单的 write/updates 吗?在尝试添加或设置该字段之前,我是否真的必须先查找该项目以查看它是否具有该字段,因为我无法使用空数组实例化该字段?
ADD 操作处理 create/update 逻辑,但仅支持数字和集合。您正在尝试添加字符串类型 'S'。您需要将此字符串包装在一个数组中,并将其作为字符串集 'SS' 传递。您也不需要等号。
您的 UpdateExpression 和 ExpressionAttributeValues 应如下所示:
UpdateExpression: 'ADD socialAccounts :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
'SS': [socialAccountId]
}
},
可以找到有关更新项目的更多信息here
所以这应该很简单...
我想将字符串附加到 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 不能对数组类型字段进行简单的 write/updates 吗?在尝试添加或设置该字段之前,我是否真的必须先查找该项目以查看它是否具有该字段,因为我无法使用空数组实例化该字段?
ADD 操作处理 create/update 逻辑,但仅支持数字和集合。您正在尝试添加字符串类型 'S'。您需要将此字符串包装在一个数组中,并将其作为字符串集 'SS' 传递。您也不需要等号。
您的 UpdateExpression 和 ExpressionAttributeValues 应如下所示:
UpdateExpression: 'ADD socialAccounts :socialAccountId',
ExpressionAttributeValues: {
':socialAccountId': {
'SS': [socialAccountId]
}
},
可以找到有关更新项目的更多信息here