简体   繁体   English

如何使用 ConditionExpression 更新一个属性值而不影响其他属性 - DynamoDB

[英]How to update one attribute value using ConditionExpression without affecting other attributes - DynamoDB

I need to update Attribute Values of table items of DynamoDB.我需要更新 DynamoDB 表项的属性值。 repeats section should update only if usersIDs Array consist with the user ID of current user.仅当usersIDs数组与当前用户的用户 ID一致时,重复部分才应更新。

Then I created ConditionExpression and run it.然后我创建了 ConditionExpression 并运行它。

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.但是当我添加这个 ConditionExpression 时,其他属性也会因此而影响。 So How Can I fix this.那么我该如何解决这个问题。 Do i need to create seperate UpdateExpression?我需要创建单独的 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:一个操作可以有 0-1 个条件,这适用于整个操作:

Docs: If the condition expression evaluates to true, the operation succeeds; Docs:如果条件表达式为真,则操作成功; otherwise, the operation fail否则,操作失败

Here are two options, given your current structure:鉴于您当前的结构,这里有两个选项:

  1. Make two update operations, one unconditional and one conditional.进行两次更新操作,一种是无条件的,一种是有条件的。 (note that list_append(Moment.repeats, :repeateUsers) does not guard against adding duplicate entries) (注意list_append(Moment.repeats, :repeateUsers)不能防止添加重复条目)
  2. Make a query to retrieve the current record and, after determining what needs to be written, make one unconditional update operation.进行查询以检索当前记录,并在确定需要写入的内容后,进行一次无条件更新操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不使用 ExpressionAttributeValues 的 DynamoDB ConditionExpression - DynamoDB ConditionExpression without using ExpressionAttributeValues 如何使用ConditionExpression检查dynamodb中是否已存在非键属性? - how to check if non-key attribute already exists in dynamodb using ConditionExpression? 如何使用 dynamodbiface.expression 为 `dynamodb.UpdateItemInput ` 设置 `ConditionExpression` - How to set `ConditionExpression` for `dynamodb.UpdateItemInput ` using dynamodbiface.expression DynamoDb putItem ConditionExpression在c ++中 - 添加或更新 - DynamoDb putItem ConditionExpression in c++ - add or update 不使用 Key 属性的 DynamoDB 更新操作 - DynamoDB Update operation without using the Key attribute 带有ConditionExpression的DynamoDb - DynamoDb with ConditionExpression DynamoDB 使用 conditionExpression 而不是键更新项目 - DynamoDB update item with conditionExpression instead of key 在Java中使用DynamoDB的PutItem ConditionExpression(避免替换属性) - Working with DynamoDB's PutItem ConditionExpression in Java (avoid replacement of attributes) DynamoDB:如果属性已存在,如何更新除一个属性之外的项目 - DynamoDB: How to update an item except for one attribute if the attribute already exists DynamoDB - 如何在一次更新中创建映射并向其添加属性 - DynamoDB - How to create map and add attribute to it in one update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM