DynamoDB’s UpdateItem operation allows for granular modification of individual attributes within an item, but its true power lies in the use of expression attributes.
Let’s see this in action. Imagine we have a Products table with a ProductID (partition key) and Category (sort key). We want to update an item.
// Original item
{
"ProductID": "BOOK#123",
"Category": "FICTION",
"Title": "The Great Novel",
"Price": 19.99,
"Tags": ["fantasy", "adventure"],
"Rating": 4.5
}
We can use UpdateItem with expressions to modify this item without needing to read and rewrite the entire object.
Here’s how we can add a new tag, remove the rating, and update the price:
aws dynamodb update-item \
--table-name Products \
--key '{"ProductID": {"S": "BOOK#123"}, "Category": {"S": "FICTION"}}' \
--update-expression "SET Price = :p, Tags = list_append(Tags, :t) REMOVE Rating" \
--expression-attribute-values '{
":p": {"N": "21.99"},
":t": {"SS": ["new-release"]}
}' \
--return-values UPDATED_NEW
The UPDATE-EXPRESSION is the core of this operation. It’s a string that tells DynamoDB how to change the item. We have four main types of operations within expressions:
-
SET: This is the most common. It can assign a new value to an attribute, create an attribute if it doesn’t exist, or modify parts of existing attributes like list elements or map entries. If the attribute already exists,SEToverwrites it. If you’re setting a list or set, you often uselist_append()orset_add()functions. For example,SET Tags = list_append(Tags, :new_tag)appends a new tag to the existingTagslist. -
REMOVE: This operation deletes attributes from an item. You simply list the attributes to be removed.REMOVE Ratingin the example above will delete theRatingattribute entirely. -
ADD: This is specifically for adding values to numeric attributes or adding elements to sets. It only works if the attribute doesn’t already exist or if it’s a set. If you try toADDto a non-existent numeric attribute, it will create it with that value. If you try toADDa string to a list, it will fail. For sets,ADD Tags :new_tag_setwould add elements from:new_tag_setto theTagsset. Note thatADDdoes not overwrite existing values; it attempts to merge or increment. -
DELETE: This expression is used to remove elements from a set or elements from a list at a specific index. For example,DELETE Tags :tags_to_removewould remove all elements specified in:tags_to_removefrom theTagsset. It’s important to distinguishDELETE(for sets/lists) fromREMOVE(for entire attributes).
The expression-attribute-values is where you define the actual data that goes into your expression. You use placeholders (like :p, :t in the example) and then map them to the concrete values. This is crucial for security and correctness, preventing injection issues and allowing for complex data types.
The levers you control are the UPDATE-EXPRESSION itself and the expression-attribute-values. You can combine multiple operations within a single UPDATE-EXPRESSION, separated by spaces. For instance, SET Price = :p, Tags = list_append(Tags, :t) REMOVE Rating performs three distinct actions in one go.
What most people don’t realize is that SET can also be used to replace entire lists or maps. If you wanted to replace the Tags list entirely, you’d use SET Tags = :new_tags_list where :new_tags_list is a full list value. This is different from list_append which adds to an existing list.
The next thing you’ll likely encounter is dealing with conditional updates, where you only want to perform the update if certain conditions are met.