Condition Expression

Documentation

Although I introduced you to the Condition expression in the context of PutItem, this concept applies to other API as well. The condition expression must evaluate to true in order for the operation to succeed; otherwise, the operation fails.

condition-expression

The condition expression must evaluate to true in order for the operation to succeed; otherwise, the operation fails. To create the expression you may use:

  • Functions attribute_exists | attribute_not_exists | attribute_type | contains | begins_with | size
  • Comparison operators = | <> | < | > | <= | >= | BETWEEN | IN
  • Logical operators AND | OR | NOT Comparison operators and create compound expressions using logical operators AND , OR and NOT

Example: How to prevent update with PutItem

You can use the function attribute_not_exist(Primary Key) If the attribute exists, it will lead to ConditionCheckFailedException otherwise the item will be created.

aws dynamodb put-item \
    --table-name   Employee  \
    --item '{    
        "LoginAlias": {"S": "irenes"},  
        "FirstName": {"S": "Irene"},    
        "LastName": {"S": "Sherwin"},     
        "ManagerLoginAlias": {"S": "rajs"}, 
        "Designation": {"S": "accountant"},    
        "Skills": {"SS": ["general accounting"]}    
    }'   \
    --condition-expression "attribute_not_exists(LoginAlias)"  

Condition expressions is a very powerful idea and is used in multiple scenarios.