How to Update Item in DynamoDB: Syntax and Example
To update an item in DynamoDB, use the
UpdateItem operation with a key to identify the item and an UpdateExpression to specify attribute changes. You can add, modify, or remove attributes atomically without replacing the entire item.Syntax
The UpdateItem operation requires specifying the TableName, the Key of the item to update, and an UpdateExpression that defines the changes. You can also use ExpressionAttributeValues to safely pass values and ReturnValues to get the updated item back.
- TableName: The name of your DynamoDB table.
- Key: The primary key of the item you want to update.
- UpdateExpression: A string that describes the attributes to update.
- ExpressionAttributeValues: Values used in the update expression.
- ReturnValues: What to return after update (e.g.,
ALL_NEWreturns the updated item).
json
{
"TableName": "YourTableName",
"Key": { "PrimaryKeyName": { "S": "PrimaryKeyValue" } },
"UpdateExpression": "set AttributeName = :value",
"ExpressionAttributeValues": { ":value": { "S": "NewValue" } },
"ReturnValues": "ALL_NEW"
}Example
This example updates the "Status" attribute of an item with primary key "OrderId" in the "Orders" table to "Shipped" and returns the updated item.
javascript
const { DynamoDBClient, UpdateItemCommand } = require("@aws-sdk/client-dynamodb"); const client = new DynamoDBClient({ region: "us-east-1" }); async function updateOrderStatus() { const params = { TableName: "Orders", Key: { "OrderId": { S: "12345" } }, UpdateExpression: "set #st = :s", ExpressionAttributeNames: { "#st": "Status" }, ExpressionAttributeValues: { ":s": { S: "Shipped" } }, ReturnValues: "ALL_NEW" }; try { const command = new UpdateItemCommand(params); const response = await client.send(command); console.log("Updated item:", response.Attributes); } catch (error) { console.error(error); } } updateOrderStatus();
Output
Updated item: { OrderId: { S: '12345' }, Status: { S: 'Shipped' } }
Common Pitfalls
- Not specifying the correct
Keywill cause the update to fail because DynamoDB won't find the item. - Forgetting to use
ExpressionAttributeNameswhen your attribute names are reserved words or contain special characters. - Using
UpdateExpressionsyntax incorrectly, such as missing thesetkeyword or wrong placeholders. - Not handling conditional updates properly can overwrite data unintentionally.
json
/* Wrong: Missing 'set' keyword in UpdateExpression */ UpdateExpression: "Status = :s", /* Right: Correct UpdateExpression syntax */ UpdateExpression: "set Status = :s"
Quick Reference
| Parameter | Description |
|---|---|
| TableName | Name of the DynamoDB table |
| Key | Primary key of the item to update |
| UpdateExpression | Expression defining attribute updates |
| ExpressionAttributeValues | Values used in the update expression |
| ExpressionAttributeNames | Optional names for attributes (for reserved words) |
| ReturnValues | What to return after update (e.g., ALL_NEW) |
Key Takeaways
Use UpdateItem with a Key and UpdateExpression to change item attributes in DynamoDB.
Always use ExpressionAttributeNames for reserved words and ExpressionAttributeValues for safe value substitution.
ReturnValues set to ALL_NEW returns the updated item after the operation.
Check your UpdateExpression syntax carefully to avoid errors.
Ensure the Key matches the item you want to update exactly.