我尝试使用 put
覆盖 DynamoDB 中的一个项目(使用名为 username
的主键),如下所示:
console.log('writing commands',existingCommands,message.username);
var t2 = performance();
var writeParams = {
Item: {
username: message.username,
commands: existingCommands // Sorry for the confusing name, due to deepExtend existingCommands are the new commands
},
TableName: TableName
};
docClient.put(writeParams, function(err, data){
if(err){
console.error('error',err);
} else {
console.log('write result',data);
var t3 = performance();
console.info('delete & write performance',(t3-t2).toFixed(3));
}
// End function
context.done();
});
适用于:
- 在
用户名
不存在的位置插入新项目。 更新与我尝试插入的项目的架构相匹配的项目,例如,我正在尝试插入该项目:
{ "username":"ausin441062133", "commands": { "command1":"command", "command2":"command" } }
如果有一个项目与架构和用户名匹配,它将被覆盖,即
{
"username":"ausin441062133",
"commands": {
"command1":"I will be overwritten",
"command2":"I will be overwritten"
}
}
但是,当存在具有确切用户名但架构不同的项目时,它不起作用,即
{
"username":"ausin441062133",
"commands": {
"command1":"I will NOT be overwritten"
}
}
如果现有项目与用户名匹配,我需要使用什么命令来覆盖现有项目?
最佳答案
最终,正如 Dmitry 建议的那样,update
可以工作,但它需要一些不同的参数,而不是 put
这是我的代码:
// Step 3 write command back
console.log('writing commands',existingCommands,message.username);
var t2 = performance();
var updateParams = {
Key: {
username: message.username
},
UpdateExpression: "set commands = :c",
ExpressionAttributeValues: {
":c":existingCommands
},
ReturnValues: "UPDATED_NEW",
TableName: TableName
};
docClient.update(updateParams, function(err, data){
if(err){
console.error('error',err);
} else {
console.log('write result',data);
var t3 = performance();
console.info('delete & write performance',(t3-t2).toFixed(3));
}
// End function
context.done();
});
关于node.js - 如何在 Lambda 上使用 Nodejs 在 DynamoDB 中覆盖具有不同架构但相同键的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300570/