Querying and scanning a DynamoDB table - AWS SDK for JavaScript

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

Querying and scanning a DynamoDB table

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • 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:

  • Set up the project environment to run these Node.js examples, and install the required AWS SDK for JavaScript and third-party modules. Follow the instructions on GitHub.

  • Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Shared config and credentials files in the AWS SDKs and Tools Reference Guide.

  • Create a DynamoDB table whose items you can access. For more information about creating a DynamoDB table, see Creating and using tables in DynamoDB.

Important

These examples use ECMAScript6 (ES6). This requires Node.js version 13.x or higher. To download and install the latest version of Node.js, see Node.js downloads..

However, if you prefer to use CommonJS syntax, please refer to JavaScript ES6/CommonJS syntax.

Note

For information about the data types used in these examples, see Supported data types and naming rules in Amazon DynamoDB.

Querying a table

Create a Node.js module with the file name query.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. To access DynamoDB, create a DynamoDB client 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 QueryCommand method of the DynamoDB service object.

import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new QueryCommand({ KeyConditionExpression: "Flavor = :flavor", // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes and // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors ExpressionAttributeValues: { ":flavor": { S: "Key Lime" }, ":searchKey": { S: "no coloring" }, }, FilterExpression: "contains (Description, :searchKey)", ProjectionExpression: "Flavor, CrustType, Description", TableName: "Pies", }); const response = await client.send(command); response.Items.forEach(function (pie) { console.log(`${pie.Flavor.S} - ${pie.Description.S}\n`); }); return response; };

To run the example, enter the following at the command prompt.

node query.js

This example code can be found here on GitHub.

Scanning a table

Create a Node.js module with the file name scan.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. To access DynamoDB, create a DynamoDB client 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 ScanCommand method of the DynamoDB service object.

import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new ScanCommand({ FilterExpression: "CrustType = :crustType", // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes and // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors ExpressionAttributeValues: { ":crustType": { S: "Graham Cracker" }, }, ProjectionExpression: "Flavor, CrustType, Description", TableName: "Pies", }); const response = await client.send(command); response.Items.forEach(function (pie) { console.log(`${pie.Flavor.S} - ${pie.Description.S}\n`); }); return response; };

To run the example, enter the following at the command prompt.

node scan.js

This example code can be found here on GitHub.