UpdateItem lets you change data in a DynamoDB table without replacing the whole item. It saves time and keeps your data accurate.
0
0
UpdateItem basics in DynamoDB
Introduction
You want to change just one or two fields in a record without touching the rest.
You need to add a new attribute to an existing item.
You want to increase a number, like adding 1 to a counter.
You want to remove an attribute from an item.
You want to update an item only if it meets certain conditions.
Syntax
DynamoDB
UpdateItem {
TableName: "TableName",
Key: { "PrimaryKey": {S: "KeyValue"} },
UpdateExpression: "SET #attr = :val",
ExpressionAttributeNames: {"#attr": "AttributeName"},
ExpressionAttributeValues: {":val": {S: "NewValue"}}
}UpdateExpression tells DynamoDB what to change.
Use ExpressionAttributeNames and ExpressionAttributeValues to safely use attribute names and values.
Examples
Change the Name attribute of user with ID 123 to "Alice".
DynamoDB
UpdateItem {
TableName: "Users",
Key: { "UserID": {S: "123"} },
UpdateExpression: "SET #name = :newName",
ExpressionAttributeNames: {"#name": "Name"},
ExpressionAttributeValues: {":newName": {S: "Alice"}}
}Increase the Stock attribute by 5 for product A1.
DynamoDB
UpdateItem {
TableName: "Products",
Key: { "ProductID": {S: "A1"} },
UpdateExpression: "ADD Stock :inc",
ExpressionAttributeValues: {":inc": {N: "5"}}
}Remove the DeliveryDate attribute from order 789.
DynamoDB
UpdateItem {
TableName: "Orders",
Key: { "OrderID": {S: "789"} },
UpdateExpression: "REMOVE DeliveryDate"
}Sample Program
This updates the Position of employee E001 to "Manager".
DynamoDB
UpdateItem {
TableName: "Employees",
Key: { "EmployeeID": {S: "E001"} },
UpdateExpression: "SET #pos = :newPos",
ExpressionAttributeNames: {"#pos": "Position"},
ExpressionAttributeValues: {":newPos": {S: "Manager"}}
}OutputSuccess
Important Notes
UpdateItem only changes specified attributes; others stay the same.
If the item does not exist, UpdateItem can create it if you use the right parameters.
Always use ExpressionAttributeNames and ExpressionAttributeValues to avoid errors with reserved words.
Summary
UpdateItem changes parts of an item without replacing it all.
Use UpdateExpression to specify what to change.
It is useful for adding, changing, or removing attributes safely.