What this thread does
Do first: append a unique _id tie-breaker to cursor.sort() and run cursor.explain("executionStats") on the exact filtered page query before selecting an index.
Short answer
As of November 2026, this card follows current MongoDB docs; 8.2.12, July 22, 2026 is a dated release baseline. Resume using the entire ordered tuple rather than an increasing skip. Equality-filter prefixes followed by sort keys are a candidate index; the actual explain plan must show whether the query avoids blocking sort.
Choose / don't choose
- Keyset pagination: choose for sequential traversal; don't promise arbitrary page-number jumps without a separate navigation strategy (range queries).
- Diagnostic hint: overrides default index selection; choose to compare plans; don't force it permanently from one timing sample (hint).
- Disk spill: choose only if a legitimate non-indexed sort needs it; don't confuse avoiding an error with an efficient pagination plan (allowDiskUse).
Not for: Atlas connection failures; see mongodb-atlas-connection-timeout-from-deployed-app-ip-access-list.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| Sort parameter | Ordered sort document ending in unique _id; 1 ascending, -1 descending | Current MongoDB docs, November 2026 | sort |
| Index hint | Index name or index specification document; compare hinted and unhinted plans | Current MongoDB docs, November 2026 | hint |
.allowDiskUse(false) | Prohibit writing temporary files to disk; current docs describe a 100 megabyte limit | Current MongoDB docs, November 2026 | allowDiskUse |
executionStats explain mode | Executes the winning plan to completion | Current MongoDB docs, November 2026 | explain |
Order of operations
1. Define immutable, consistently typed sort keys and a fixed filter. Example application shape: tenant equality, descending createdAt, descending _id; candidate compound pattern {tenant:1, createdAt:-1, _id:-1}. This instantiates the documented equality-prefix rule (index sorting). 2. For the next page, retain the tenant filter and add the lexicographic range below. lastTime, lastId, and pageSize are application inputs, with original BSON types retained. The OR handles ties; using only _id < lastId would not resume the timestamp order.
const next = {tenant: tenantId, $or: [
{createdAt: {$lt: lastTime}},
{createdAt: lastTime, _id: {$lt: lastId}}
]};
db.items.find(next).sort({createdAt:-1, _id:-1}).limit(pageSize);
3. For ascending order use $gt. Save the tuple from the last returned document. Keep filter, collation and direction identical across requests (range pagination). 4. Explain first-page and deep-page queries, with and without the actual candidate hint. Inspect IXSCAN and any FETCH; COLLSCAN means collection scanning. Compare nReturned, totalKeysExamined, and totalDocsExamined: a scan vastly exceeding the bounded page signals residual filtering or poor bounds. An index name alone does not prove efficiency (results).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Sort exceeded memory limit | Sort exceeded the memory limit without opting in to external sorting | Match filter/sort index; allow spill only deliberately when indexing cannot eliminate the sort | Scott Weiner, February 17, 2021, current behavior |
I get duplicate values | The reply recommends a unique tie breaker as the last sort field | Add _id and resume by the full tuple | Vc Jhala, January 31, 2024; Steeve Juneau reply |
Missing
- The linked source does not confirm: SORT means blocking sort
- The linked source does not confirm: Zero documents examined can indicate coverage.
- A unique tie-breaker gives deterministic ordering for equal keys, not a fixed dataset across page requests. Concurrent updates to sort keys can move records between pages; this recipe assumes stable keys and does not define a cross-request snapshot contract.
Started from a post by @MongoDB, packaged by the editor. Original post