0
0
DynamoDBquery~5 mins

Expression attribute values in DynamoDB

Choose your learning style9 modes available
Introduction

Expression attribute values let you safely use values in DynamoDB queries without errors or conflicts.

When you want to insert or update an item with specific values.
When you need to filter items using conditions with values.
When you want to avoid conflicts with reserved words in DynamoDB.
When you want to reuse values in multiple parts of a query safely.
Syntax
DynamoDB
ExpressionAttributeValues = {
  ":key1": value1,
  ":key2": value2
}

Each key starts with a colon (:), like ":val".

Values can be strings, numbers, booleans, or other DynamoDB data types.

Examples
Use this to set the value "Alice" for a name attribute.
DynamoDB
ExpressionAttributeValues = {
  ":name": {"S": "Alice"}
}
Use this to set the value 30 for an age attribute.
DynamoDB
ExpressionAttributeValues = {
  ":age": {"N": "30"}
}
Use this to set a boolean value true for an isActive attribute.
DynamoDB
ExpressionAttributeValues = {
  ":isActive": {"BOOL": true}
}
Sample Program

This command updates the Name attribute of the user with UserId "123" to "Bob" using expression attribute values.

DynamoDB
aws dynamodb update-item \
  --table-name Users \
  --key '{"UserId": {"S": "123"}}' \
  --update-expression "SET #n = :nameVal" \
  --expression-attribute-names '{"#n": "Name"}' \
  --expression-attribute-values '{":nameVal": {"S": "Bob"}}'
OutputSuccess
Important Notes

Always prefix keys in ExpressionAttributeValues with a colon (:).

Use ExpressionAttributeNames to avoid conflicts with reserved words.

Expression attribute values help prevent injection attacks by separating data from code.

Summary

Expression attribute values store values safely for DynamoDB queries.

They use keys starting with a colon (:), like ":val".

They help avoid errors and reserved word conflicts.