[英]Update DynamoDB item via GSI
我想通過node.js更新AWS DynamoDB中的現有項目。 我只有要更新的項目的二級索引值。 我無法訪問主索引...
Id: primary index
CallId: global secondary index
CallStatus: normal field
我只想通過使用CallId(不使用鍵)來更新CallStatus。
我嘗試了不同的方法,例如:
但是這些方法都不適合我。 我想是因為我沒有正確使用它們。 任何幫助表示贊賞:-)。
“掃描和更新”方法的代碼示例:
var docClient = new aws.DynamoDB.DocumentClient();
var params = {
TableName: 'myTable',
FilterExpression: 'CallId = :c',
ExpressionAttributeValues: {
':c': callSid
}
};
docClient.scan(params, function (err, result) {
if (err) {
console.error("Unable to query item. Error JSON:", JSON.stringify(err));
} else {
console.log(result);
// Update call status
var params = {
TableName: "myTable",
Key: {
"Id": result.Items[0].Id
},
UpdateExpression: "set CallStatus = :s",
ExpressionAttributeValues: {
":s": callStatus
},
ReturnValues: "UPDATED_NEW"
};
docClient.update(params, function (err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err));
} else {
console.log("Update item succeeded:", JSON.stringify(data));
}
});
}
});
好的,我發現了我的錯誤。 上面的更新之后的調用應在AWS Lambda會話關閉時發生,因此DynamoDB的異步更新從未發生過...
現在工作的代碼如下:
var params = {
TableName: 'table',
IndexName: 'CallId-index',
KeyConditionExpression: 'CallId = :id',
ExpressionAttributeValues: {
':id': callSid
}
};
docClient.query(params, function (err, result) {
if (err) {
console.log("Unable to query item. Error JSON:", JSON.stringify(err));
} else {
var params = {
TableName: "table",
Key: {
"Id": result.Items[0].Id
},
UpdateExpression: "set CallStatus = :s",
ExpressionAttributeValues: {
":s": callStatus
},
ReturnValues: "UPDATED_NEW"
};
docClient.update(params, function (err, data) {
if (err) {
console.log("Unable to update item. Error JSON:", JSON.stringify(err));
} else {
// do sth. else
}
});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.