Reputation: 692
I want to update an item in DynamoDB only if the new item has a more recent date than the existing item. Currently, I'm querying for the existing item, doing the comparison in my code, and then writing to db. I was wondering if there was a way to have DynamoDB do the checking and updating in single query.i have used conditionalexpression but it's not updating value even if condition satisfies. I'm working with nodejs.
Upvotes: 4
Views: 996
Reputation: 692
I have found solution by using
Expected
clause of dynamodb. so below is my code.
var params;
params = {
Key: {
"HashKey": {
S: "Value"
}
},
TableName: 'TableName',
AttributeUpdates: {
"Attr1": {
Value: {
S: "Value1"
}
}
},
Expected: {
"Attr2": {
"ComparisonOperator": "EQ",
"AttributeValueList": [ {S: "Value2"} ]
}
}
};
dynamodb_client.updateItem(params, function(err, data) {
if (err){
console.log(err);
} else {
console.log(data);
}
});
so above code updates value of Attr1 to Value 1 if Attr2 is equal to Value2
Upvotes: 2