DynamoDB更新一个字段,该字段的值为JSON,python中的键为integer/num字符串



我正在我的dynamodb表中存储一个具有以下结构的项。

Item = {"response": [
{
"answers": {
"11-18": 0,
"19-24": 0
}
}
]}

我想逐步更新响应[0]。answer.11-18。我的命令是:

table_resource.update_item(
Key={
'id': 123
},
UpdateExpression="set response[0].answers.11-18 = response[0].answers.11-18 + :inc",
ExpressionAttributeValues={":inc": 1},
ReturnValues="UPDATED_NEW",
)

我得到以下错误。

An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "11", near: ".11-"

嵌套属性11-18以数字开头,因此不能在更新表达式中使用它。

如果属性名称以数字开头或包含空格、特殊字符或保留字,则必须使用表达式属性名称来替换表达式中该属性的名称。

您可以使用占位符ExpressionAttributeNames来解决此问题。

最新更新