How to separate multiple clauses in a DynamoDB Update Expression
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In AWS DynamoDB, executing update operations on items often requires the use of Update Expressions. These expressions allow you to specify how attributes in an item should be modified. In instances where multiple changes need to be made simultaneously, separating multiple clauses correctly in an Update Expression becomes essential. This article explores how to construct these expressions effectively, including specific syntax and examples.
Understanding Update Expressions
Update Expressions provide a way to modify attributes of items in DynamoDB tables. These expressions consist of various clauses that perform specific operations:
- SET: Modify or add a particular attribute.
- REMOVE: Delete an attribute from the item.
- ADD: Increment/decrement a numeric attribute.
- DELETE: Remove an element from a set.
Correctly combining these clauses is critical for performing complex updates in a single operation.
Structure of Update Expressions
Update Expressions may contain several clauses, each relating to different attributes or operations. To prevent ambiguity and ensure proper execution, each clause type is separated by a space and a distinct keyword which relates to the operation being performed. More specifically:
- Use a space to separate SET, REMOVE, ADD, and DELETE from the rest of the expression.
- Each clause separated by a comma.
An example of a basic Update Expression is:
- `SET` modifies or adds `attribute1` and `attribute2`.
- `REMOVE` deletes `attribute3`.
- `ADD` increments or adds to `attribute4`.
- `DELETE` removes elements from `attribute5`.
- Assign a new value to `price` and `stock`.
- Remove the `saleDate` attribute.
- Increment the `salesCount` by 5.
- Delete the `reviews` set attribute for a specific ID.
- Attribute Names and Placeholders: Use placeholders for attribute values to prevent conflicts with reserved keywords. For example, replace direct values in expressions with `:value`.
- Attribute Names Aliasing: Use `#` to alias attribute names to preserve keywords, e.g., `#n` for an attribute `name`.
- Consistent Structure: Keep clauses logically grouped and concise for readability.
Related reading
- How to set a boolean field in Dynamo DB?
- How to set a DynamoDB Map property value, when the map doesn''t exist yet
- How to set a Message Handler programmatically in Spring Cloud AWS SQS?
- How to set a stage name in a SAM template
- How to serialize SqlAlchemy result to JSON?
- How to set a default entity property value with Hibernate
- How to set an environment variable in Amazon EC2
- how to SET and DELETE using a single UpdateExpression with Dynamo

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.