Home > Software design >  Multiple condition expressions for each attribute when performing update in DynamoDB
Multiple condition expressions for each attribute when performing update in DynamoDB

Time:03-04

I have the following code to update one DynamoDB attribute:

val key = new AttributeValue().withS(rowAsMap.get("key").get)
val value = new AttributeValue().withS(rowAsMap.get("newValue").get)

val updItemReq: UpdateItemRequest = new UpdateItemRequest()
            .withTableName("table_name")
            .addExpressionAttributeValuesEntry(":newValue", value)
            .addExpressionAttributeNamesEntry("#val", "value")
            .addKeyEntry("key", key)
            .withUpdateExpression("set previous_value = #val, #val= :newValue")
            .withConditionExpression("#val <> :newValue")
    
ddb.updateItem(updItemReq)

This will successfully update the "value" and "previous_value" attributes if the condition expression is met (in this case if there was a modification).

What I need is to do this for other attributes, so I need a specific condition expression for each attribute to update. Can I do this without having to create as many "UpdateItemRequests" as many attributes I have?

CodePudding user response:

Sorry, no. There’s just the one condition expression

CodePudding user response:

Check this doc: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html

You can have multiple conditions in one updateItem request.

  • Related