Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.151.0

Querying and Scanning a DynamoDB Table


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to query and scan a DynamoDB table for items.

The Scenario

Querying finds items in a table or a secondary index using only primary key attribute values. You must provide a partition key name and a value for which to search. You can also provide a sort key name and value, and use a comparison operator to refine the search results. Scanning finds items by checking every item in the specified table.

In this example, you use a series of Node.js modules to identify one or more items you want to retrieve from a DynamoDB table. The code uses the SDK for JavaScript to query and scan tables using these methods of the DynamoDB client class:

Prerequisite Tasks

To set up and run this example, first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Querying a Table

This example queries a table that contains episode information about a video series, returning the episode titles and subtitles of second season episodes past episode 9 that contain a specified phrase in their subtitle.

Create a Node.js module with the file name ddb_query.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB service object. Create a JSON object containing the parameters needed to query the table, which in this example includes the table name, the ExpressionAttributeValues needed by the query, a KeyConditionExpression that uses those values to define which items the query returns, and the names of attribute values to return for each item. Call the query method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { ExpressionAttributeValues: { ':s': {N: '2'}, ':e' : {N: '09'}, ':topic' : {S: 'PHRASE'} }, KeyConditionExpression: 'Season = :s and Episode > :e', ProjectionExpression: 'Title, Subtitle', FilterExpression: 'contains (Subtitle, :topic)', TableName: 'EPISODES_TABLE' }; ddb.query(params, function(err, data) { if (err) { console.log("Error", err); } else { data.Items.forEach(function(element, index, array) { console.log(element.Title.S + " (" + element.Subtitle.S + ")"); }); } });

To run the example, type the following at the command line.

Copy
node ddb_query.js

This sample code can be found here on GitHub.

Scanning a Table

Create a Node.js module with the file name ddb_scan.js. Be sure to configure the SDK as previously shown. To access DynamoDB, create an AWS.DynamoDB service object. Create a JSON object containing the parameters needed to scan the table for items, which in this example includes the name of the table, the list of attribute values to return for each matching item, and an expression to filter the result set to find items containing a specified phrase. Call the scan method of the DynamoDB service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { ExpressionAttributeValues: { ":topic": { S: "PHRASE" } }, FilterExpression: "contains (Subtitle, :topic)", ProjectionExpression: "Title, Subtitle", TableName: "EPISODES_TABLE" }; ddb.scan(params, function(err, data) { if (err) { console.log("Error", err); } else { data.Items.forEach(function(element, index, array) { console.log(element.Title.S + " (" + element.Subtitle.S + ")"); }); } });

To run the example, type the following at the command line.

Copy
node ddb_scan.js

This sample code can be found here on GitHub.