Thus, conditional updates in DynamoDB will only allow you to update items in a table only if certain conditions match. This feature comes in handy in updating items anatomically, whereby your system will either effect all or none of your updates. Besides, conditional updates will only be successful if the item has stayed the same since you last read the item.
This blog post highlights everything about DynamoDB conditional updates. It outlines how to use conditional updates and provides examples of conditional updates in DynamoDB.
How to Use Conditional Updates in DynamoDB
To perform conditional updates in DynamoDB, use the UpdateItem operation and the ConditionExpression parameter. The parameter should take the condition that must be satisfied to necessitate the successful application of the update. Should your system fail to meet the condition for one reason or another, the update will fail and return an error.
Notably, the UpdateItem operation also supports update expressions. Operations with update expressions help specify each modification you should make on an item.
A DynamoDB conditional update examples are as follows. All the examples use Boto3, which is the AWS SDK for Python.
Example 1: Updating an Item Only if a Specific Attribute has a Particular Value.
Conditional updates help update an item only if the attribute has a particular value. A Python example of the same is shown below:
# Create a DynamoDB client
client = boto3.client('dynamodb')
# Define the primary key of the item you want to update
key = {
'id': {'N': '1234'}
}
# Define the update expression and attribute values
update_expression = 'SET #a = :val1, #b = :val2'
expression_attribute_names = {
'#a': 'attribute1',
'#b': 'attribute2'
}
expression_attribute_values = {
':val1': {'N': '5678'},
':val2': {'S': 'abcd'}
}
# Define the condition expression
condition_expression = '#a = :old_val'
condition_expression_attribute_values = {
':old_val': {'N': '1234'}
}
# Perform the update
response = client.update_item(
TableName='my-table-name',
Key=key,
UpdateExpression=update_expression,
ExpressionAttributeNames=expression_attribute_names,
ExpressionAttributeValues=expression_attribute_values,
ConditionExpression=condition_expression,
ExpressionAttributeValues=condition_expression_attribute_values
)
This utility updates the item with primary key id = 1234 in the my-table-name table. It will set the values of attribute1 and attribute2 to 5678 and abcd, respectively. However, the update will only be applied if the current value of attribute1 is 1234. If the value of attribute1 has changed since the item was last read, the update will fail and your system will return an error.
Example 2: Updating an Item Only if the Item has a Certain Attribute
The code for such a conditional update is as shown:
# Get the DynamoDB client
client = boto3.client('dynamodb')
# Set the item key and attribute values
key = {
'id': {'N': '123'}
}
update_expression = 'SET a = :a, b = :b'
expression_attribute_values = {
':a': {'N': '5'},
':b': {'S': 'abc'}
}
# Set the condition expression to check if the item has the 'c' attribute
condition_expression = 'attribute_exists(c)'
# Update the item
response = client.update_item(
TableName='my-table-name',
Key=key,
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values,
ConditionExpression=condition_expression
)
The example above demonstrates how to update an item in a DynamoDB table only if the item has a specific attribute. In this case, the condition expression will check for the existence of the c attribute using the attribute_exists function. If the item does not have the c attribute, the update will fail and return an error.
Example 3: Updating an Item Only if a Particular Attribute has a Value Exceeding a Specific Value.
Finally, you can set your command to update an item only if a particular attribute is greater than a specific value. The Python utility is as shown below;
# Get the DynamoDB client
client = boto3.client('dynamodb')
# Set the item key and attribute values
key = {
'id': {'N': '1234'}
}
update_expression = 'SET a = :a, b = :b'
expression_attribute_values = {
':a': {'N': '15'},
':b': {'S': 'abcd'}
}
# Set the condition expression to check if the 'count' attribute is greater than 15
condition_expression = 'count > :count'
expression_attribute_values[':count'] = {'N': '15'}
# Update the item
response = client.update_item(
TableName='my-table-name',
Key=key,
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values,
ConditionExpression=condition_expression
)
The illustration shows how to update an item in a DynamoDB table only if a specific attribute has a value greater than a particular value. In this case, the condition expression checks if the count attribute is greater than 15 using the > operator. If the count attribute is at most 15, the update will fail, and you will receive an error message.
Conclusion
DynamoDB’s conditional expression feature is a powerful method of specifying conditions when updating items in a table. It is helpful in many instances. For example, you can use it to ensure that an item is only updated if it hasn’t changed since it was read or that an item is only updated if a particular attribute has a specific value.