Given a DynamoDB table Users with a primary key UserID and an attribute Age, what will be the new value of Age after running this UpdateItem?
{
TableName: "Users",
Key: { "UserID": { "S": "user123" } },
UpdateExpression: "SET Age = Age + :inc",
ExpressionAttributeValues: { ":inc": { "N": "5" } },
ReturnValues: "UPDATED_NEW"
}Assume the current Age is 30.
TableName: "Users" Key: { "UserID": { "S": "user123" } } UpdateExpression: "SET Age = Age + :inc" ExpressionAttributeValues: { ":inc": { "N": "5" } } ReturnValues: "UPDATED_NEW"
Think about how the SET expression updates the existing value.
The UpdateExpression adds 5 to the current Age of 30, resulting in 35.
Which of the following UpdateExpression syntaxes correctly adds a new attribute Status with value Active?
Remember to use ExpressionAttributeValues for values in UpdateExpression.
Option A uses a placeholder :status which should be defined in ExpressionAttributeValues. This is the correct syntax.
Consider this UpdateItem request on a DynamoDB table where the attribute Score does not exist yet for the item:
{
TableName: "Games",
Key: { "GameID": { "S": "game789" } },
UpdateExpression: "SET Score = Score + :val",
ExpressionAttributeValues: { ":val": { "N": "10" } },
ReturnValues: "UPDATED_NEW"
}What will be the result?
Think about how DynamoDB handles arithmetic on missing attributes.
DynamoDB throws a ValidationException if you try to add a number to a non-existing attribute without initializing it first.
Examine this UpdateItem snippet:
{
TableName: "Employees",
Key: { "EmpID": { "S": "e123" } },
UpdateExpression: "SET #n = :name",
ExpressionAttributeNames: { "#n": "Name" },
ExpressionAttributeValues: { ":name": "John" },
ReturnValues: "UPDATED_NEW"
}Why does it fail?
Check the format of ExpressionAttributeValues.
ExpressionAttributeValues must specify the data type, e.g., { ":name": { "S": "John" } }.
You want to safely increment a numeric attribute Count in a DynamoDB item without overwriting concurrent updates. Which UpdateItem approach guarantees atomic increment?
Consider DynamoDB's atomic update operators.
The ADD operator atomically increments numeric attributes even if they don't exist yet.