I need to update Attribute Values of table items of DynamoDB. repeats section should update only if usersIDs Array consist with the user ID of current user.
Then I created ConditionExpression and run it.
var metricsParams = {
TableName: table,
Key:{
"metricsID" : metricsID,
},
UpdateExpression: "SET fans.orgID = :orgIDNew, fans.orgName = :orgNameNew, fans.noOfGamesPlayed = fans.noOfGamesPlayed + :val, Moment.datePlayed = :dateNew, Moment.monthPlayed = :monthNew, Moment.week = :weekNew, Moment.usersIDs = list_append(Moment.usersIDs, :usersNew), Moment.repeats = list_append(Moment.repeats, :repeateUsers)",
ConditionExpression: "contains(Moment.repeats, :repeateUsers)",
ExpressionAttributeValues:{
":orgIDNew": body.team.id,
":orgNameNew": body.team.domain,
":val": 1,
":dateNew" : Moment().format('LL'),
":monthNew" : Moment().format("MMMM"),
":weekNew" : Moment().format('WW'),
":usersNew" : [body.user.id],
":repeateUsers": [body.user.id]
},
ReturnValues:"UPDATED_NEW"
};
console.log("Attempting a conditional update...");
metricsDoc.update(metricsParams, function(err, data) {
if (err) {
console.error("Unable to update item. *from id update* Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded: FROM DYNAMODB METRICS**", JSON.stringify(data, null, 2));
But when I add this ConditionExpression other Attributes also affect because of this. So How Can I fix this. Do i need to create seperate UpdateExpression?
You are correct. If you have multiple conditions, you will need multple operations. An operation can have 0-1 conditions, which applies to the operation as a whole:
Docs: If the condition expression evaluates to true, the operation succeeds; otherwise, the operation fail
Here are two options, given your current structure:
list_append(Moment.repeats, :repeateUsers)
does not guard against adding duplicate entries)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.