S
Supanote
Sign in Sign up

MongoDB 8.0+ setQuerySettings: Pin Index Hints Without Redeploy

code (javascript) 1 day ago · 58 lines · 5 views
Verified Blueprint MongoDB 8.0+

Enforce persistent MongoDB 8.0+ indexHints, maxTimeMS, or reject runaway COLLSCAN query shapes cluster-wide via mongosh without redeploying.

  • ✓ Pins optimal compound index hints across primary and replica secondaries via setQuerySettings
  • ✓ Blocks dangerous unindexed query shapes instantly during incidents using reject: true
  • ✓ Persists in cluster metadata across restarts without touching application ORM code
Quick Fetch
1// MongoDB 8.0+ Cluster-Wide Persistent Index Hints & Query Firewall (mongosh)
2// Run in mongosh against your primary or mongos router — ZERO application redeploy required!
3
4const targetDb = db.getSiblingDB('commerce_prod');
5
6// 1. Pin a Specific Compound Index & Execution Timeout Onto a Regressed Query Shape
7// Planner matches structural shape (predicates, sort, projection), ignoring literal filter values
8targetDb.runCommand({
9 setQuerySettings: {
10 find: 'orders',
11 filter: {
12 merchantId: 'm_example',
13 status: { $in: ['paid', 'shipped'] },
14 createdAt: { $gte: ISODate('2026-01-01T00:00:00Z') },
15 },
16 sort: { createdAt: -1 },
17 $db: 'commerce_prod',
18 },
19 settings: {
20 indexHints: {
21 ns: { db: 'commerce_prod', coll: 'orders' },
22 allowedIndexes: [{ merchantId: 1, status: 1, createdAt: -1 }],
23 },
24 queryFramework: 'classic',
25 comment: 'INC-2048: Force ESR index over low-cardinality createdAt_1 index',
26 },
27});
29// 2. Emergency Circuit Breaker: Reject an Unbounded Unindexed Regex / COLLSCAN Shape
30targetDb.runCommand({
31 setQuerySettings: {
32 find: 'audit_logs',
33 filter: { rawPayload: { $regex: 'timeout' } },
34 $db: 'commerce_prod',
35 },
36 settings: {
37 reject: true,
38 comment: 'INC-2049: Block unindexed regex scan saturating WiredTiger tickets',
39 },
40});
42// 3. Inspect Active Cluster Query Settings & Representative Hashes
43const activeRules = targetDb.aggregate([
44 { $querySettings: { showDebugQueryShape: true } },
45 {
46 $project: {
47 queryShapeHash: 1,
48 representativeQuery: 1,
49 settings: 1,
50 },
51 },
52]).toArray();
54printjson(activeRules);
56// 4. Remove Override Once Application Query / Index Migration Ships
57// targetDb.runCommand({ removeQuerySettings: '<queryShapeHash>' });

No replies yet

Every reply is a note. Start a discussion, ask a question, or attach a code snippet.

Related MongoDB 8.0+ & Database, Cache & SQL Blueprints

Production-ready configurations and diagnostics from the verified Supanote library.

All Database, Cache & SQL →
Interactive Developer Workbench: Online JSON Formatter, Validator & Viewer

Validate, pretty-print, minify, and inspect JSON API responses with exact syntax error pinpointing.

Open JSON Formatter →

Share Note

Open
Download SVG
Social Card Preview
Open on mobile
Point your phone camera to open this note directly

Report this note

Notification