0
0
DynamoDBquery~5 mins

Multiple actions in one update in DynamoDB

Choose your learning style9 modes available
Introduction
You can change many parts of a database item at once to save time and keep data correct.
When you want to change several details of a user profile in one go.
When you need to add a new tag and update a count number together.
When you want to remove an old value and add a new one in the same update.
When you want to increase a score and set a last played time at once.
Syntax
DynamoDB
UpdateExpression: "SET attribute1 = :val1, attribute2 = :val2 REMOVE attribute3 ADD attribute4 :val4 DELETE attribute5 :val5"
ExpressionAttributeValues: { ":val1": value1, ":val2": value2, ":val4": value4, ":val5": value5 }
Use SET to add or change attributes.
Use REMOVE to delete attributes.
Use ADD to add numbers or add elements to sets.
Use DELETE to remove elements from sets.
Examples
This changes age and city, removes oldAddress, and adds 10 points.
DynamoDB
UpdateExpression: "SET age = :newAge, city = :newCity REMOVE oldAddress ADD points :pointsToAdd"
ExpressionAttributeValues: { ":newAge": 30, ":newCity": "Seattle", ":pointsToAdd": 10 }
This sets status to active and removes tag1 and tag2 from the tags set.
DynamoDB
UpdateExpression: "SET status = :newStatus DELETE tags :tagsToRemove"
ExpressionAttributeValues: { ":newStatus": "active", ":tagsToRemove": {"tag1", "tag2"} }
Sample Program
This updates the player with ID 'player123' by setting their Level to 5, updating LastLogin time, removing OldBadge attribute, and adding 50 to their Score.
DynamoDB
UpdateItem {
  TableName: "Players",
  Key: { "PlayerID": { S: "player123" } },
  UpdateExpression: "SET Level = :newLevel, LastLogin = :loginTime REMOVE OldBadge ADD Score :scoreIncrease",
  ExpressionAttributeValues: {
    ":newLevel": { N: "5" },
    ":loginTime": { S: "2024-06-01T10:00:00Z" },
    ":scoreIncrease": { N: "50" }
  },
  ReturnValues: "UPDATED_NEW"
}
OutputSuccess
Important Notes
You can combine SET, REMOVE, ADD, and DELETE in one UpdateExpression.
Make sure ExpressionAttributeValues match the placeholders in UpdateExpression.
ReturnValues: "UPDATED_NEW" returns only the changed attributes.
Summary
Multiple actions let you update many parts of an item in one request.
Use SET to add or change, REMOVE to delete, ADD to increase numbers or add to sets, DELETE to remove from sets.
This saves time and keeps your data consistent.