Over a million developers have joined DZone.

Update DynamoDB Items With Node.js

In this post, we take a look at how to update items in DynamoDB using Node.js including some examples. Read on to find out more!

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

In a previous post, we inserted items into DynamoDB using Node.js. DynamoDB also supports updating items.

We will use the Login table for the update examples.

When issuing an update, you must specify the primary key of the item you want to update.

var updateName = function(email,fullName,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set fullname = :fullname",
    ExpressionAttributeValues:{
        ":fullname":fullName
    },
    ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}


We can proceed on more advanced statements using conditional updates. Conditional updates can help us in many cases, such as handling concurrent updates. In our case, we will update an item’s full name only if it starts with a certain prefix.

var updateConditionally = function(email,fullName,prefix,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set fullname = :fullname",
ConditionExpression: "begins_with(fullname,:prefix)",
ExpressionAttributeValues:{
        ":fullname":fullName,
        ":prefix":prefix
    },
    ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}


Another feature is atomic counters. We can issue updates to a DynamoDB item and increase the attribute values. We will add an extra field called count. Also, we will add another update function, which, once called, will update the field specified, but will also increase the counter attribute. Thus, the counter attribute will represent how many times an update was performed on a specific item.

var addUpdateCounter = function(email,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set #counter = :counter",
ExpressionAttributeNames:{
        "#counter":"counter"
    },
ExpressionAttributeValues:{
        ":counter":0
    },
ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}

var updateAndIncreaseCounter = function(email,fullName,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set fullname = :fullname ADD #counter :incva",
ExpressionAttributeNames:{
        "#counter":"counter"
    },
ExpressionAttributeValues:{
        ":fullname":fullName,
        ":incva":1
    },
    ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}


And that's it! You can now update items in DynamoDB using Node.js As usual, you can find the source code on GitHub.

Related Refcard:

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:
examples ,update ,node.js ,dynamodb ,database

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}