我更新了放置項目成功執行的項目並獲得了預期的結果。
注意:在DynamoDB local上執行的測試。
ALL_NEW - 返回該項目的所有屬性,因爲它們在UpdateItem操作後出現 。
示例代碼: -
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "post";
var paramsPut = {
TableName: table,
Item: {
"postId": '16',
"Pos": {
"key": "1a", "a": "1"
}
}
};
var paramsUpdate = {
TableName: "post",
Key: {
"postId": "16"
},
UpdateExpression: "SET Pos.#key = :keyVal, Pos.b = :keyVal2",
ExpressionAttributeNames: {
"#key": "key"
},
ExpressionAttributeValues: {
":keyVal": "1",
":keyVal2": "2"
},
ReturnValues: "ALL_NEW"
};
console.log("Adding a new item...");
docClient.put(paramsPut, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
console.log("Then Updating the item...");
docClient.update(paramsUpdate, 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));
}
});
}
});
輸出: -
Adding a new item...
Added item: {}
Then Updating the item...
UpdateItem succeeded: {"Attributes":{"Pos":{"a":"1","b":"2","key":"1"},"postId":
"16"}}
真的,這是一個一致性的問題,所以我不認爲你希望看到相同的結果一個實際的DDB羣集 – NightWolf