【发布时间】:2018-07-09 07:28:49
【问题描述】:
我有一个场景,如果项目不存在,我想创建它,或者更新一个项目 - 如果它已经存在,则增加一个总数。
我在拆分这两个操作时遇到了问题,所以我现在尝试在单个命令中使用 UpdateItem 来完成这两个操作。
我尝试了 3 种不同的方法都不起作用,它们有下面列出的不同错误,问题似乎是创建地图并尝试在单个命令中更新它 - 我的更新参数应该是什么样的?
尝试一:
{
TableName: TableName,
Key: {
'key': key
},
UpdateExpression: `
ADD #total :change
, mapname.#type.#total :one
`,
ExpressionAttributeValues: {
':change': change,
':one': 1
},
ExpressionAttributeNames: {
'#type': 'dynamicstring',
'#total': 'total'
}
};
错误为:ValidationException: The document path provided in the update expression is invalid for update
尝试二:
{
TableName: TableName,
Key: {
"key": key
},
UpdateExpression: `
SET custommap = if_not_exists(custommap, :emptyMap)
SET #total = #total + :change,
custommap.#type.#total = custommap.#type.#total + :one
`,
ExpressionAttributeValues: {
':change': change,
':one': 1,
':emptyMap': {
'M': {
'dynamicstring': {
'M': {
'total': {
'N': 0
}
}
}
}
}
},
ExpressionAttributeNames: {
'#type': 'dynamicstring',
'#total': 'total'
}
}
错误为:ValidationException: Invalid UpdateExpression: The "SET" section can only be used once in an update expression;
那么当我使用 UpdateItem 在 Item 中创建或更新(增加)地图时,什么语法是正确的?
谢谢
【问题讨论】:
标签: amazon-web-services amazon-dynamodb