我需要更新我的aws-dynamo数据库上的列表。我创建了一个带分区键的表:电子邮件。然后我成功地将一些电子邮件ID插入表中。现在我的桌子就像这样
manaf1@gmail.com
manaf2@gmail.com
manaf3@gmail.com
然后,我尝试用新密钥更新表格"详细信息"它的值是一个列表。这是我的代码
var AWS = require("aws-sdk");
var params =
{
TableName: "manaftable1",
Key: { email: "manaf1@gmail.com" },
UpdateExpression: "set #details = list_append (#details, :detailsinput)",
ExpressionAttributeNames: {
"#details": "details"
},
ExpressionAttributeValues: {
":detailsinput":{ "id": "1","mob": "978956" }
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.update(params, function (err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
但是我得到了这样的错误
{
"message": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M",
"code": "ValidationException",
"time": "2016-10-26T11:04:60.756",
"requestId": "SN0NPRHDFHUKBBJHOVI0DFHHRQNSO5AEMVJFFGHF9ASFHUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
我需要在更新后像这样回复
column1:email
第2栏:详情
manaf1@gmail.com | [{" ID":" 1""暴民":" 978956"},{" ID":& #34; 2""暴民":" 767886"}]
manaf2@gmail.com |
与我的代码相关的问题是什么?
答案 0 :(得分:2)
作为list_append
says的AWS文档:
新元素必须包含在列表中,例如将2添加到a 列表,操作数将是[2]
所以你需要追加一个对象数组,而不仅仅是一个对象:
var params =
{
TableName: "manaftable1",
Key: { email: "manaf1@gmail.com" },
UpdateExpression: "set #details = list_append (#details, :detailsinput)",
ExpressionAttributeNames: {
"#details": "details"
},
ExpressionAttributeValues: {
":detailsinput": [{ "id": "1","mob": "978956" }]
}
};
如果对象中不存在该属性,则可以使用if_not_exists
操作数:
var params =
{
TableName: "manaftable1",
Key: { email: "manaf1@gmail.com" },
UpdateExpression: "set if_not_exists(#details, []) set #details = list_append (#details, :detailsinput)",
ExpressionAttributeNames: {
"#details": "details"
},
ExpressionAttributeValues: {
":detailsinput": [{ "id": "1","mob": "978956" }]
}
};
答案 1 :(得分:1)
当项目中存在details
属性为 NOT 时,更新表达式应该只有SET
而没有list_append。
UpdateExpression : "SET #details = :details"
当商品中的details
属性存在且新更新需要在列表中添加其他元素时,list_append
可用于添加新元素列表。
UpdateExpression : "set #details = list_append (#details, :details)"
答案 2 :(得分:0)
同时使用list_append()
和if_not_exists()
附加到可能不存在的列表列:
var params = {
TableName: "manaftable1",
Key: { email: "manaf1@gmail.com" },
UpdateExpression: "set #details = list_append(if_not_exists(#details, :empty_list), :detailsinput)",
ExpressionAttributeNames: {
"#details": "details"
},
ExpressionAttributeValues: {
":detailsinput": [{ "id": "1","mob": "978956" }],
":empty_list": []
}
};