DynamoDB: Get Unique Values (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
DynamoDB has no DISTINCT operation for arbitrary attributes. Scan projected values and deduplicate them in your application, or maintain a separate lookup item for each value at write time.
Install @aws-sdk/client-dynamodb and @aws-sdk/lib-dynamodb, use an ES module on a supported Node.js runtime, and configure an AWS Region and credentials. The examples use an existing Orders table with String keys pk and sk unless an index or timestamp key is described.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, ScanCommand } from "@aws-sdk/lib-dynamodb"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); const regions = new Set(); let lastKey; do { const page = await ddb.send(new ScanCommand({ TableName: "Orders", ProjectionExpression: "#region", ExpressionAttributeNames: { "#region": "region" }, ExclusiveStartKey: lastKey, })); for (const item of page.Items ?? []) { if (typeof item.region === "string") regions.add(item.region); } lastKey = page.LastEvaluatedKey; } while (lastKey && Object.keys(lastKey).length > 0); console.log([...regions]);
Projection reduces response payload, not read capacity. This example assumes region is a String; the set consumes memory proportional to the number of distinct values.
Reference: AWS Scan API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- DynamoDB: Query Greater Than
- DynamoDB: Not Begins With
- DynamoDB: Get Last 10 Records
- DynamoDB: Delete Multiple Items in Javascript
- DynamoDB: GetItem
- DynamoDB BatchWriteItem in Typescript
- DynamoDB: Get By ID
- DynamoDB: Query Ends With
- DynamoDB: Query Date Range
- DynamoDB: Like
- DynamoDB: Get Multiple Items
- DynamoDB: Get Last Inserted Item
- DynamoDB: Query JSON
- DynamoDB: Increment Counter
- DynamoDB: Delete All Items With Partition Key
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today