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, SET overwrites it. If you’re setting a list or set, you often use list_append() or set_add() functions. For example, SET Tags = list_append(Tags, :new_tag) appends a new tag to the existing Tags list.

  • REMOVE: This operation deletes attributes from an item. You simply list the attributes to be removed. REMOVE Rating in the example above will delete the Rating attribute 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 to ADD to a non-existent numeric attribute, it will create it with that value. If you try to ADD a string to a list, it will fail. For sets, ADD Tags :new_tag_set would add elements from :new_tag_set to the Tags set. Note that ADD does 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_remove would remove all elements specified in :tags_to_remove from the Tags set. It’s important to distinguish DELETE (for sets/lists) from REMOVE (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.

Want structured learning?

Take the full Dynamodb course →