Amazon web services DynamoDB条件put
假设我想添加一条如下所示的记录:Amazon web services DynamoDB条件put,amazon-web-services,amazon-dynamodb,Amazon Web Services,Amazon Dynamodb,假设我想添加一条如下所示的记录: { id:876876, username:example, email:xxxxxx@xxx.com, phone:xxxxxxx, created_at:current_date, updated_at:current_date } 我想用id检查记录是否存在created\u date不应该被修改updated\u date应该有current\u date 我们可以使用put方法而不必进行get item调用吗?创建新项或使用条件表达式更新现有项。喜欢
{
id:876876,
username:example,
email:xxxxxx@xxx.com,
phone:xxxxxxx,
created_at:current_date,
updated_at:current_date
}
我想用id检查记录是否存在created\u date
不应该被修改updated\u date
应该有current\u date
我们可以使用put方法而不必进行get item调用吗?创建新项或使用条件表达式更新现有项。喜欢使用而不是使用 如果散列键(主键)不存在,
putItem
和updateItem
创建新记录。如果记录已经存在,putItem
将完全覆盖现有行,但updateItem
仅更新传入的属性UpdateExpression而不是整个记录
在您的情况下,使用if\u not\u exists()
函数检查在字段创建的是否已经存在。如果存在,则不会覆盖在
处创建的
更新表达式:“设置电子邮件=:电子邮件,#创建地址=如果不存在(#创建地址,:创建地址),#更新地址=:更新地址”
示例片段
var params = {
ExpressionAttributeNames: {
"#email": "email",
"#created_at": "created_at",
"#updated_at": "updated_at"
},
ExpressionAttributeValues: {
":email": {
S: "test2@grr.la"
},
":created_at": {
S: date.toISOString()
},
":updated_at": {
S: date.toISOString()
}
},
Key: {
"id": {
S: "T1"
}
},
ReturnValues: "ALL_NEW",
TableName: "stack",
UpdateExpression: "SET #email = :email, #created_at = if_not_exists(#created_at, :created_at), #updated_at = :updated_at"
};
ddb.updateItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data);
})
是的,你可以。到目前为止你试过什么?您是否已经有代码可用于创建不带条件的项目?感谢您的回复。但我需要的是避免覆盖一个创建的字段。可以更新其他字段。我可以简单地调用get item并检查记录是否存在,然后为created_at字段添加相同的值,并用当前日期时间更新updated_at字段。在这里,我需要对dynamodb进行两次点击,但我希望通过单次put调用实现这一点。有什么办法吗?更喜欢使用updateItem来实现您的需求。它将避免执行GET操作,然后执行update操作。也更新了答案。