DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Update DynamoDB Items With Node.js

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!

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
Aug. 10, 16 · Database Zone · Tutorial
Like (3)
Save
Tweet
48.86K Views

Join the DZone community and get the full member experience.

Join For Free

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:

Node.js

Node.js

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Deploy Apache Kafka With Kubernetes
  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • What Are Cookies in Servlets?
  • A First Look at CSS When and Else Statements

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo