DynamoDB UpdateItem in Node.js (AWS SDK v3)
UpdateItem changes specific attributes of one item (and creates the item if it doesn't exist), leaving everything else untouched. In AWS SDK v3 you send an UpdateItemCommand with an UpdateExpression.
Code
import {DynamoDBClient, UpdateItemCommand} from '@aws-sdk/client-dynamodb';
const client = new DynamoDBClient({region: 'us-east-1'});
async function updateSong() {
const command = new UpdateItemCommand({
TableName: 'Music',
Key: {
Artist: {S: 'Arturo Sandoval'},
SongTitle: {S: 'Cubano Chant'}
},
UpdateExpression: 'SET Genre = :genre, #yr = :year ADD Awards :inc',
ExpressionAttributeNames: {'#yr': 'Year'},
ExpressionAttributeValues: {
':genre': {S: 'Latin Jazz'},
':year': {N: '1994'},
':inc': {N: '1'}
},
ReturnValues: 'ALL_NEW'
});
const response = await client.send(command);
console.log(response.Attributes); // the item after the update
return response.Attributes;
}
updateSong();Explanation
Key— the full primary key of the item to update.UpdateExpression— one or more clauses:SETassigns attributes (Genre = :genre).Yearis reserved, so it's aliased to#yr.ADDon a number does an atomic increment (ADD Awards :inc) — safe under concurrency, no read-modify-write race.REMOVEdeletes an attribute;DELETEremoves elements from a set.
ExpressionAttributeValues— the:placeholder→ typed-value map.ReturnValues—"ALL_NEW"returns the full item after the update (alsoUPDATED_NEW,ALL_OLD,UPDATED_OLD, orNONE).- Upsert semantics —
UpdateItemcreates the item if the key doesn't exist. Add aConditionExpression(e.g.attribute_exists(Artist)) to update-only.
Do it visually
The DynamoDB Expression Builder builds SET / ADD / REMOVE clauses with the name/value maps and copies runnable code.
To edit items in a GUI — change a field, review the generated UpdateExpression, copy the SDK v3 code — download DynoTable.
Related guides
- DynamoDB update expressions —
SET,ADD,REMOVE,DELETE, and idioms. - Understanding ReturnValues — what each
ReturnValuesoption gives you.