Range-based pagination continues from a last-seen ordering key. A complete deterministic order prevents ambiguous continuation.
Before you start
You should understand documents, collections and indexes. Sketch representative documents and the reads and writes they must support. MongoDB-specific examples assume a collection with the shown fields; deployment topology, permissions and existing indexes can affect the operational behavior being discussed.
The practical goal is to reason through this situation: Sort by createdAt and ID, then continue after that pair. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Create deterministic ordering
Use createdAt plus _id rather than timestamp alone.
Step 2: Encode every order key
Continuation compares the full previous tuple under matching directions.
Step 3: Validate cursor input
Reject malformed types and define concurrent-change behavior.
Worked scenario
Sort by createdAt and ID, then continue after that pair.
Two records share a timestamp at the page boundary. A cursor containing only that timestamp cannot distinguish them, potentially skipping or repeating one. Including the unique tie-breaker completes the order. Range pagination avoids increasing skip work, but does not by itself provide a stable snapshot across requests.
Common mistake
Large skip offsets can increase work, and timestamp ties can duplicate pages.
Verify the behavior
Test timestamp ties, deleted boundary records and inserts between pages.
Interview exercise
Define a cursor contract.
Answer and reasoning
Include every ordering key, validate cursor input and specify behavior under concurrent insertion or deletion.
Continue learning
Compare the scenario with the MongoDB interview questions and test your understanding with the MongoDB MCQs. For terminology and implementation details, consult the reference material.