Get All Items from DynamoDB in Node.js (AWS SDK v3)
There is no "select all" in DynamoDB — reading a whole table means a Scan you paginate to the end. Each response carries at most 1 MB; you loop, feeding LastEvaluatedKey back as ExclusiveStartKey, until no key comes back.
Code
import {DynamoDBClient, ScanCommand} from '@aws-sdk/client-dynamodb';
const client = new DynamoDBClient({region: 'us-east-1'});
const items = [];
let lastEvaluatedKey;
do {
const response = await client.send(
new ScanCommand({
TableName: 'Music',
ExclusiveStartKey: lastEvaluatedKey
})
);
items.push(...(response.Items ?? []));
lastEvaluatedKey = response.LastEvaluatedKey;
} while (lastEvaluatedKey);
console.log(`Table holds ${items.length} items`);Explanation
- The loop is the point — a single
Scancall does NOT return the table; it returns up to 1 MB. Stopping after the first response is the classic "my scan is missing items" bug. Only an absentLastEvaluatedKeymeans you've read everything. - Memory —
itemsholds the entire table. For big tables, process each page inside the loop (write to a file, stream, aggregate) instead of accumulating. - Faster on big tables: parallel scan — split the work with
Segment/TotalSegments: run N workers, each scanning its own segment with its own pagination loop. Same total cost, much lower wall-clock time. - Cost — a full scan reads (and bills) every byte of every item, every time you run it. Trim the response with
ProjectionExpression(fewer attributes returned — the read cost stays the same), and don't put a scan on a hot path; that's what Query and access-pattern design are for. - Filtering while you're at it? A
FilterExpressiondrops items after they're read (and billed) — see Scan with a filter.
Do it visually
Eyeballing a table doesn't need a script: download DynoTable to browse any table in a paginated grid — it pages through scans for you, no loop to get wrong. And before you scan-and-pay, the DynamoDB pricing calculator shows what a full read of your table actually costs.
Related examples
- Get all items in Python — the same full read with boto3's paginator.
- Get all items with the AWS CLI — the CLI pages for you.
- DynamoDB Scan in Node.js — scanning with a
FilterExpression. - Parallel scans — Segment/TotalSegments, worker counts, and when to bother.
- Why is my DynamoDB Scan slow and expensive? — the cost model and how to avoid it.
- DynamoDB ProvisionedThroughputExceededException — reading the whole table is the classic way to hit it.
- "The provided starting key is invalid" — a mangled resume key in the pagination loop.
References
- Scan — Amazon DynamoDB API Reference
- Scanning tables in DynamoDB — Amazon DynamoDB Developer Guide
- DynamoDB read and write operations (capacity unit consumption) — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.