Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Step 3: Create, Read, Update, and Delete an Item

In this step, you perform read and write operations on an item in the Movies table.

To learn more about reading and writing data, see Working with Items in DynamoDB.

Step 3.1: Create a New Item

In this step, you add a new item to the Movies table.

  1. Copy and paste the following program into a file named MoviesItemOps01.js:

    Copy
    var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient(); var table = "Movies"; var year = 2015; var title = "The Big New Movie"; var params = { TableName:table, Item:{ "year": year, "title": title, "info":{ "plot": "Nothing happens at all.", "rating": 0 } } }; console.log("Adding a new item..."); docClient.put(params, function(err, data) { if (err) { console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("Added item:", JSON.stringify(data, null, 2)); } });

    Note

    The primary key is required. This code adds an item that has a primary key (year, title) and info attributes. The info attribute stores sample JSON that provides more information about the movie.

  2. To run the program, type the following command:

    node MoviesItemOps01.js

Step 3.2: Read an Item

In the previous program, you added the following item to the table:

Copy
{ year: 2015, title: "The Big New Movie", info: { plot: "Nothing happens at all.", rating: 0 } }

You can use the get method to read the item from the Movies table. You must specify the primary key values, so you can read any item from Movies if you know its year and title.

  1. Copy and paste the following program into a file named MoviesItemOps02.js:

    Copy
    var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient() var table = "Movies"; var year = 2015; var title = "The Big New Movie"; var params = { TableName: table, Key:{ "year": year, "title": title } }; docClient.get(params, function(err, data) { if (err) { console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); } });
  2. To run the program, type the following command:

    node MoviesItemOps02.js

Step 3.3: Update an Item

You can use the update method to modify an existing item. You can update values of existing attributes, add new attributes, or remove attributes.

In this example, you perform the following updates:

  • Change the value of the existing attributes (rating, plot).

  • Add a new list attribute (actors) to the existing info map.

The item changes from this:

Copy
{ year: 2015, title: "The Big New Movie", info: { plot: "Nothing happens at all.", rating: 0 } }

To the following:

Copy
{ year: 2015, title: "The Big New Movie", info: { plot: "Everything happens all at once.", rating: 5.5, actors: ["Larry", "Moe", "Curly"] } }
  1. Copy and paste the following program into a file named MoviesItemOps03.js:

    Copy
    var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient() var table = "Movies"; var year = 2015; var title = "The Big New Movie"; // Update the item, unconditionally, var params = { TableName:table, Key:{ "year": year, "title": title }, UpdateExpression: "set info.rating = :r, info.plot=:p, info.actors=:a", ExpressionAttributeValues:{ ":r":5.5, ":p":"Everything happens all at once.", ":a":["Larry", "Moe", "Curly"] }, ReturnValues:"UPDATED_NEW" }; console.log("Updating the item..."); docClient.update(params, function(err, data) { if (err) { console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2)); } });

    Note

    This program uses UpdateExpression to describe all updates you want to perform on the specified item.

    The ReturnValues parameter instructs DynamoDB to return only the updated attributes ("UPDATED_NEW").

  2. To run the program, type the following command:

    node MoviesItemOps03.js

Step 3.4: Increment an Atomic Counter

DynamoDB supports atomic counters, where you use the update method to increment or decrement the value of an existing attribute without interfering with other write requests. (All write requests are applied in the order in which they are received.)

The following program shows how to increment the rating for a movie. Each time you run it, the program increments this attribute by one.

  1. Copy and paste the following program into a file named MoviesItemOps04.js:

    Copy
    var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient() var table = "Movies"; var year = 2015; var title = "The Big New Movie"; // Increment an atomic counter var params = { TableName:table, Key:{ "year": year, "title": title }, UpdateExpression: "set info.rating = info.rating + :val", ExpressionAttributeValues:{ ":val":1 }, ReturnValues:"UPDATED_NEW" }; console.log("Updating the item..."); docClient.update(params, function(err, data) { if (err) { console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2)); } });
  2. To run the program, type the following command:

    node MoviesItemOps04.js

Step 3.5: Update an Item (Conditionally)

The following program shows how to use UpdateItem with a condition. If the condition evaluates to true, the update succeeds; otherwise, the update is not performed.

In this case, the item is updated only if there are more than three actors in the movie.

  1. Copy and paste the following program into a file named MoviesItemOps05.js:

    Copy
    var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient() var table = "Movies"; var year = 2015; var title = "The Big New Movie"; // Conditional update (will fail) var params = { TableName:table, Key:{ "year": year, "title": title }, UpdateExpression: "remove info.actors[0]", ConditionExpression: "size(info.actors) > :num", ExpressionAttributeValues:{ ":num":3 }, ReturnValues:"UPDATED_NEW" }; console.log("Attempting a conditional update..."); docClient.update(params, function(err, data) { if (err) { console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2)); } });
  2. To run the program, type the following command:

    node MoviesItemOps05.js

    The program should fail with the following message:

    The conditional request failed

    This is because the movie has three actors in it, but the condition is checking for greater than three actors.

  3. Modify the program so that the ConditionExpression looks like this:

    Copy
    ConditionExpression: "size(info.actors) >= :num",

    The condition is now greater than or equal to 3 instead of greater than 3.

  4. Run the program again. The updateItem operation should now succeed.

Step 3.6: Delete an Item

You can use the delete method to delete one item by specifying its primary key. You can optionally provide a ConditionExpression to prevent the item from being deleted if the condition is not met.

In the following example, you try to delete a specific movie item if its rating is 5 or less.

  1. Copy and paste the following program into a file named MoviesItemOps06.js:

    Copy
    var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }); var docClient = new AWS.DynamoDB.DocumentClient(); var table = "Movies"; var year = 2015; var title = "The Big New Movie"; var params = { TableName:table, Key:{ "year":year, "title":title }, ConditionExpression:"info.rating <= :val", ExpressionAttributeValues: { ":val": 5.0 } }; console.log("Attempting a conditional delete..."); docClient.delete(params, function(err, data) { if (err) { console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2)); } });
  2. To run the program, type the following command:

    node MoviesItemOps06.js

    The program should fail with the following message:

    The conditional request failed

    This is because the rating for this particular movie is greater than 5.

  3. Modify the program to remove the condition from params.

    Copy
    var params = { TableName:table, Key:{ "title":title, "year":year } };
  4. Run the program again. Now, the delete succeeds because you removed the condition.