Update an Item in a DynamoDB Table

Updating an item involves modifying one or more of its attributes. You can change existing attributes, add new ones, or remove some attributes from an item. Below are examples in Node.js and Python:

Node.js:

const params = {  
    TableName: table,  
    Key: {  
       'UserID': userID  
    },  
    UpdateExpression: 'set UserName = :n',  
    ExpressionAttributeValues: {  
       ':n': 'Jane Doe'  
    },  
    ReturnValues: 'UPDATED_NEW'  
};  
      
docClient.update(params, function(err, data) {  
    if (err) console.error('Error', err);  
    else  console.log('UpdateItem succeeded:', data);  
});

This Node.js code updates the 'UserName' attribute for a specified 'UserID' in 'UserTable' and logs the updated information.

Python:

response = table.update_item(  
    Key={  
       'UserID': 'user_001'  
    },  
    UpdateExpression='SET UserName = :val',  
    ExpressionAttributeValues={  
       ':val': 'Jane Doe'  
    },  
    ReturnValues='UPDATED_NEW'  
)  
print("UpdateItem succeeded:", response)

The Python code performs an update operation for the 'UserName' of a specific 'UserID' in 'UserTable' and prints the outcome of the update.