gpt4 book ai didi

amazon-web-services - DynamoDB : UpdateItem, 忽略 ExpressionAttributeValues 中的空值

转载 作者:行者123 更新时间:2023-12-04 23:12:17 27 4
gpt4 key购买 nike

我正在使用 DynamoDB UpdateItem更新我的数据库中的记录。像这样的基本功能对我有用。

var user = {
userID: '123213',
name: 'John Doe',
age: 12,
type: 'creator'
};
var params = {
TableName:table,
Key:{
"UserID": user.userID
},
UpdateExpression: "set Name = :r, Age=:p, Type=:a",
ExpressionAttributeValues:{
":r":user.name,
":p":user.age,
":a":user.type
},
ReturnValues:"UPDATED_NEW"
};

docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
});

但是...

如果我只想更新一个属性,即名称,如下所示:
 var user = {
userID: '123213',
name: 'John Smith'
};
var params = {
TableName:table,
Key:{
"UserID": user.userID
},
UpdateExpression: "set Name = :r, Age=:p, Type=:a",
ExpressionAttributeValues:{
":r":user.name,
":p":user.age,
":a":user.type
},
ReturnValues:"UPDATED_NEW"
};

它给了我这样的错误

ExpressionAttributeValues cannot be NULL.



我知道我可以动态生成 UpdateExpression通过检查用户中的值来获取字符串,如下所示:
for (var key in user) {
if (user.hasOwnProperty(key)) {
...add to DynamicUpdateExpression..
}
}

但是有没有办法告诉 updateItem 忽略空值而只更新 name ?

最佳答案

这是一个简单得多的答案。

当您将 ExpressionAttributeValues 视为一个对象时,它会起作用。

这是代码:

params.TableName = ddbTable;
params.UpdateExpression = "set LastPostedDateTime = :l" ;
if (req.body.AttachmentDescription) { params.UpdateExpression += ", AttachmentDescription = :d"; }
if (req.body.AttachmentURL) { params.UpdateExpression += ", AttachmentURL = :a"; }

因此,首先我们使用简单的连接技术构建表达式,如果值可以传递。

然后我们提供值:
params.ExpressionAttributeValues = {};
params.ExpressionAttributeValues[':l'] = formattedDate ;
if (req.body.AttachmentDescription) { params.ExpressionAttributeValues[':d']= req.body.AttachmentDescription ; }
if (req.body.AttachmentURL) { params.ExpressionAttributeValues[':a']= req.body.AttachmentURL ; }

困难在于 ExpressionAttributeValues,在这里,我们将其视为一个对象,如果我们首先将其定义为一个对象,则可以添加到该对象中,因此 {}。

然后,如果对象还没有属性名称,它会添加它,然后添加值。

最终结果是您可以拥有非常宽的平面记录,因为您的记录可以使用可变字段名称进行扩展。 IE。这个应用程序列出了一个 URL 和描述符。使用可变字段名称,我可以向同一记录添加更多 URL 和描述符。最终会有一个内存限制,但这种类型的应用程序,对于一些变量字段,对于我的应用程序来说已经足够了。

关于amazon-web-services - DynamoDB : UpdateItem, 忽略 ExpressionAttributeValues 中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45842363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com