MongoDB Query Shape Optimization: Sort, Pagination, Operators and Sharded Targeting

Parent: MongoDB Performance Troubleshooting · Published reference · snapshot 2026-10-19 · skill mongodb-expert/references/mongodb-query-shape-optimization.md

Also known as: $in and $or Index Bounds and Union Plans, Blocking Sort and Index-Backed Sort, Count and Distinct and Sampling Optimization, Cursor and Read-Path Tuning (batchSize and maxTimeMS and read preference), Pagination Patterns (skip-limit vs range-keyset), Projection Optimization and Covered Query Design, Query Operator Anti-Patterns ($regex and negation and $where and $expr and $exists), Sharded Query Targeting and Scatter-Gather

↓ Facts as markdownall context files

The shape of a find or aggregate query decides whether the planner can use an index: index-backed sorts avoid the blocking SORT stage and its memory limit, keyset pagination replaces deep skip, $regex, $ne, $nin, $where and $expr constrain index bounds, $in and $or become point bounds or index unions, projection can make a query covered, count, distinct, $sample and cursor batchSize/maxTimeMS carry their own costs, and a sharded query is targeted only when the shard key is in the filter. Compiled from MongoDB manual and Atlas pages plus the run's verified claims.

These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.

Definitions

Structure and components

How it works

How-to and procedures

Examples and snippets

Measurements and reference values

Problems, failure modes and limitations

Comparisons and alternatives

Facts and statements

Related concepts

Children

Frontier under this node: $function and server-side JavaScript hardening, Client-side operation timeout (CSOT) (2.80), Collation-based case-insensitive equality, Counter-document pattern, Find projection vs $project aggregation coverage, Hashed vs ranged shard key query trade-offs, Multikey index coverage and multiKeyPaths, Multikey index sort bounds, Read concern and causal consistency with secondary reads, Sparse and partial indexes for $exists, Tailable cursors and maxAwaitTimeMS

← the whole tree · 3D view· how to read this page