MongoDB 8.0+ setQuerySettings: Pin Index Hints Without Redeploy

javascript
// MongoDB 8.0+ Cluster-Wide Persistent Index Hints & Query Firewall (mongosh)
// Run in mongosh against your primary or mongos router — ZERO application redeploy required!

const targetDb = db.getSiblingDB('commerce_prod');

// 1. Pin a Specific Compound Index & Execution Timeout Onto a Regressed Query Shape
// Planner matches structural shape (predicates, sort, projection), ignoring literal filter values
targetDb.runCommand({
  setQuerySettings: {
    find: 'orders',
    filter: {
      merchantId: 'm_example',
      status: { $in: ['paid', 'shipped'] },
      createdAt: { $gte: ISODate('2026-01-01T00:00:00Z') },
    },
    sort: { createdAt: -1 },
    $db: 'commerce_prod',
  },
  settings: {
    indexHints: {
      ns: { db: 'commerce_prod', coll: 'orders' },
      allowedIndexes: [{ merchantId: 1, status: 1, createdAt: -1 }],
    },
    queryFramework: 'classic',
    comment: 'INC-2048: Force ESR index over low-cardinality createdAt_1 index',
  },
});

// 2. Emergency Circuit Breaker: Reject an Unbounded Unindexed Regex / COLLSCAN Shape
targetDb.runCommand({
  setQuerySettings: {
    find: 'audit_logs',
    filter: { rawPayload: { $regex: 'timeout' } },
    $db: 'commerce_prod',
  },
  settings: {
    reject: true,
    comment: 'INC-2049: Block unindexed regex scan saturating WiredTiger tickets',
  },
});

// 3. Inspect Active Cluster Query Settings & Representative Hashes
const activeRules = targetDb.aggregate([
  { $querySettings: { showDebugQueryShape: true } },
  {
    $project: {
      queryShapeHash: 1,
      representativeQuery: 1,
      settings: 1,
    },
  },
]).toArray();

printjson(activeRules);

// 4. Remove Override Once Application Query / Index Migration Ships
// targetDb.runCommand({ removeQuerySettings: '<queryShapeHash>' });
MongoDB 8.0+ setQuerySettings: Pin Index Hints Without Redeploy
Note
Notification