AWS DynamoDB update item using multiple condition expression

Praveen Raghav picture Praveen Raghav · Mar 13, 2018 · Viewed 10.1k times · Source

I have a DynamoDB Table and I want a particular value to be updated using multiple condition expression. Can we do that? My code:-

        dynamodb = session.resource('dynamodb')
        table = dynamodb.Table('gold-images')
        response = table.update_item(
            Key={
                    'AccountRegionOS' : AccOs,
                    'CreationDate' : cred
                },
            UpdateExpression="set is_active = :r",
            ConditionExpression="CreationDate < :num",
            ExpressionAttributeValues={
                ':num' : last_month,
                ':r': "No"
            },
            ReturnValues="UPDATED_NEW"

I want Condition expression to be

        dynamodb = session.resource('dynamodb')
        table = dynamodb.Table('gold-images')
        response = table.update_item(
            Key={
                    'AccountRegionOS' : AccOs,
                    'CreationDate' : cred
                },
            UpdateExpression="set is_active = :r",
            ConditionExpression=("CreationDate < :num") & ("AMIID = :ami"),
            ExpressionAttributeValues={
                ':num' : last_month,
                ':r': "No",
                ':ami' : i
            },
            ReturnValues="UPDATED_NEW"

Answer

Khalid T. picture Khalid T. · Mar 13, 2018

The condition expression is a string and the logical operators are AND, OR and NOT. So, you'll need to remove outer parentheses and replace & with AND:

ConditionExpression = "CreationDate < :num AND AMIID = :ami"

See Condition Expressions.