How to Use Update Expression in DynamoDB: Syntax and Example
In DynamoDB, use the
UpdateExpression parameter to specify attribute changes like setting, adding, or removing values. Combine it with ExpressionAttributeNames and ExpressionAttributeValues to safely reference attributes and values in your update operation.Syntax
The UpdateExpression defines how to modify attributes in an item. It supports keywords like SET, REMOVE, ADD, and DELETE. Use ExpressionAttributeNames to substitute attribute names and ExpressionAttributeValues to provide values safely.
Example parts:
- SET: Assign or update attribute values.
- REMOVE: Delete attributes.
- ADD: Add numbers or append to sets.
- DELETE: Remove elements from sets.
plaintext
UpdateExpression = "SET #attrName = :val" ExpressionAttributeNames = {"#attrName": "AttributeName"} ExpressionAttributeValues = {":val": {"S": "NewValue"}}
Example
This example updates a user's age and adds a new tag to their tags set in a DynamoDB table named Users. It shows how to use UpdateExpression with placeholders for attribute names and values.
python
import boto3 # Initialize DynamoDB client client = boto3.client('dynamodb') # Update item in Users table response = client.update_item( TableName='Users', Key={ 'UserId': {'S': 'user123'} }, UpdateExpression='SET #age = :newAge ADD #tags :newTag', ExpressionAttributeNames={ '#age': 'Age', '#tags': 'Tags' }, ExpressionAttributeValues={ ':newAge': {'N': '30'}, ':newTag': {'SS': ['premium']} }, ReturnValues='UPDATED_NEW' ) print(response['Attributes'])
Output
{"Age": {"N": "30"}, "Tags": {"SS": ["premium"]}}
Common Pitfalls
Common mistakes when using UpdateExpression include:
- Not using
ExpressionAttributeNamesfor reserved words or special characters in attribute names. - Forgetting to prefix attribute names with
#and values with:in the expression. - Using incorrect data types in
ExpressionAttributeValues. - Not specifying
ReturnValuesif you want to see updated attributes.
Always test your update expressions carefully to avoid syntax errors or unexpected updates.
plaintext
Wrong: UpdateExpression='SET Age = :newAge' ExpressionAttributeValues={':newAge': {'N': '30'}} Right: UpdateExpression='SET #age = :newAge' ExpressionAttributeNames={'#age': 'Age'} ExpressionAttributeValues={':newAge': {'N': '30'}}
Quick Reference
| Keyword | Purpose | Example |
|---|---|---|
| SET | Assign or update attribute values | SET #name = :val |
| REMOVE | Delete attributes from item | REMOVE #oldAttr |
| ADD | Add numbers or append to sets | ADD #count :inc |
| DELETE | Remove elements from sets | DELETE #tags :oldTags |
Key Takeaways
Use UpdateExpression with SET, REMOVE, ADD, or DELETE to modify DynamoDB item attributes.
Always use ExpressionAttributeNames and ExpressionAttributeValues to avoid conflicts and injection.
Prefix attribute names with # and values with : in your update expressions.
Specify ReturnValues='UPDATED_NEW' to get the updated attributes after the update.
Test update expressions carefully to avoid syntax errors and data type mismatches.