node.js AWS dynamodb updateItem

是否可以通过updateItem实现以下几点:1.如果DynamoDB中不存在属性,则添加属性2.如果属性存在于DynamoDB中,则更新属性3.如果属性未包含在属性中,请保留这些属性的参数。

以下是一个示例:这是DynamoDB中的对象:

{ id: "1234", variable1: "hello", variable2: "world" } 

这是我想要更新的input:

 { id: "1234", variable1: "hello2", variable23: "dog" // the variable name "variable23" could be anything } 

以下是我想要实现的DynamoDB中的更新项目:

 { id: "1234", variable1: "hello2", variable2: "world", variable23: "dog" } 

“variable23”可以是任何variables名称作为input。

请帮忙! 我使用node.js,我真的很感激,如果有人能告诉我一些代码如何实现这一点。

谢谢!

这正是AWS.DynamoDB.DocumentClient的update方法的作用。

关于如何在Node.js中使用AWS SDK for JavaScript的update方法,已经有了一个示例代码。

例如:

 'use strict'; const aws = require('aws-sdk'); // It is recommended that we instantiate AWS clients outside the scope of the handler // to take advantage of connection re-use. const docClient = new aws.DynamoDB.DocumentClient(); exports.handler = (event, context, callback) => { const params = { TableName: "MYTABLE", Key: { "id": "1" }, UpdateExpression: "set variable1 = :x, #MyVariable = :y", ExpressionAttributeNames: { "#MyVariable": "variable23" }, ExpressionAttributeValues: { ":x": "hello2", ":y": "dog" } }; docClient.update(params, function(err, data) { if (err) console.log(err); else console.log(data); }); };