What you'll learn
- Each Query or Scan call reads at most 1 MB (before any filter), so you must paginate even without a Limit.
- Limit controls items evaluated, not necessarily items returned after filtering.
- Pass the LastEvaluatedKey back as the ExclusiveStartKey to get the next page.
Section 01
Scrolling the Pokegear
When you scan or query, Limit caps the items DynamoDB evaluates before applying a filter. The returned item count can therefore be smaller or even zero. Continue while LastEvaluatedKey is present; only an absent or empty key establishes that traversal is finished. To get the next page, you start exactly where you left off by passing that key as the ExclusiveStartKey.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, ScanCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({ region: "ap-south-1" });
const docClient = DynamoDBDocumentClient.from(client);
// Fetching Page 2
await docClient.send(new ScanCommand({
TableName: "JohtoPokemon",
Limit: 10,
ExclusiveStartKey: { Name: "Meganium" } // This came from the previous request's LastEvaluatedKey
}));Ready · 5 steps · Sprig
This animation plays when you scroll to it. Press Play to start it now.
All steps as text
- Your app sends Scan to the JohtoPokemon table. Limit 3, no ExclusiveStartKey.
- P1 returns Chikorita, Bayleef, Meganium.
- This call consumed 0.5 RCU.
- The page holds 3 items and ends with LastEvaluatedKey {"Name":"Meganium"}. Pass it as ExclusiveStartKey to continue.
- DynamoDB responds with success: the same first page again, so the list never advances.
Practice
Your turn
Page 1 stopped at
{ Name: "Meganium" }. Pass that key back as the ExclusiveStartKey so the next request resumes exactly where the last one ended.Add
ExclusiveStartKey: { Name: "Meganium" } to fetch the next page.Tab inserts two spaces. Press Escape, then Tab, to leave the editor. Control or Command plus Enter runs the code.
Ctrl/⌘ Enter to run · simulated, no AWS callsSimulated — no AWS calls
Check
Check your understanding
Quick Quiz · one question, as many tries as you need.
Which parameter do you use to tell DynamoDB where to start reading the next page of results?
Choose one answer.