Blog
AdvancedReferenceDatabases

MongoDB 9.0 query controls - per-command querySettings, queryKnobs and per-shape maxTimeMS

MongoDB 9.0 lets a single find, distinct or aggregate carry its own querySettings document, adds queryKnobs to override internal parameters per query shape, and lets a stored maxTimeMS bound a whole shape - plus the precedence rules between inline and stored settings.

Applies to
MongoDB9.0
establishedUpdated Reviewed
advancedAssumes production experience with the basics.

The story in one sentence: settings that used to live only at the cluster level can now travel with a single command, and settings that used to bound a single command can now live at the shape level. Three pieces make that up.

Inline querySettings on a command

find, distinct and aggregate accept a querySettings document in 9.0. The setting applies to that execution only - nothing is stored, nothing applies to the next query with the same shape:

// try a particular index for this execution only
db.orders.find(
  { status: "paid", currency: "EUR", amount: { $gte: 10 } },
  { querySettings: { indexHints: { allowedIndexes: ["status_1_currency_1"] } } }
)

That is the answer to “does this index actually help” without creating a stored rule first - run the command with the hint, read the explain, decide. The shape-level path still exists through setQuerySettings, and when both apply, the stored cluster setting wins on any field they both specify - the inline document is not a way to overrule stored settings.

queryKnobs: internal parameters, per shape

queryKnobs is a query setting that overrides internal server parameters for a single query shape - the parameters that previously required a setParameter against the whole process. It needs feature compatibility version 9.0 or later, and it is aimed at exactly the situation that used to force a global flag: one shape that needs different costing or memory behaviour from the fleet.

// bound one recurring reporting shape without touching global parameters
db.adminCommand({
  setQuerySettings: {
    find: "reports",
    filter: { "metrics.week": { $eq: "$$NOW" } }
  },
  settings: {
    querySettings: {
      indexHints: { allowedIndexes: ["metrics_week_1"] }
    }
  }
})

Per-shape maxTimeMS

setQuerySettings accepts maxTimeMS in 9.0, which puts a time budget on every query matching a shape - not just the one call where you remembered to pass it. Where a reporting query that usually finishes in seconds occasionally runs for minutes, the shape-level budget caps the tail without an application change:

db.adminCommand({
  setQuerySettings: { find: "reports", filter: { kind: "nightly" } },
  settings: { maxTimeMS: 5000 }
})

Both queryKnobs and per-shape maxTimeMS sit behind FCV 9.0 - a cluster partway through an upgrade will not accept them until the FCV is bumped.

Where it breaks

  • Assuming inline beats stored - the stored cluster setting wins field-for-field; an inline setting that contradicts a stored one silently loses, and EXPLAIN is the way to see which was applied.
  • Forgetting FCV - queryKnobs and per-shape maxTimeMS are rejected below FCV 9.0, which is most pre-upgrade clusters and any cluster mid-upgrade.
  • Over-broad shapes - a shape is the normalised form of the query; a setting meant for one endpoint can catch every query that normalises the same way. Inspect planCacheShapes / $queryStats before storing one.
  • maxTimeMS on a write path - the budget applies to read shapes; do not expect it to govern a transaction.

What this is not for

  • General tuning - these are surgical controls for individual shapes; planner behaviour, index design and the engine page’s fundamentals come first.
  • Replacing application timeouts - per-shape maxTimeMS is a server-side floor, not a substitute for client-side deadline propagation.

MongoDB release support

4 supported
ReleaseLatestReleased
9.09.0.8Sept 2026
8.38.3.17May 2026
8.08.0.38Oct 2024
7.07.0.48Aug 2023

Data as of 2026-11-21 from endoflife.date. Refresh with node scripts/fetch-versions.mjs.

Where MongoDB sits

How this component relates to the rest of the stack. Items without a link are not covered here yet.

Depends on

Must be present for this to run

Deployed with

Commonly run alongside

Alternatives

Solves a similar problem differently