{
  "id": "database/mongodb-shard-key-missing-in-query",
  "signature": "MongoServerError: Query targeting more than one shard but the query does not contain the shard key",
  "signature_zh": "MongoServerError：查询针对多个分片，但查询不包含分片键",
  "regex": "Query targeting more than one shard but the query does not contain the shard key",
  "domain": "database",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A query issued against a sharded MongoDB collection does not include the shard key in the filter, forcing the query to be broadcast to all shards, which is inefficient and may hit a configurable limit or be disallowed by recent MongoDB versions.",
  "root_cause_type": "generic",
  "root_cause_zh": "针对分片 MongoDB 集合的查询在过滤条件中不包含分片键，导致查询被广播到所有分片，效率低下，可能触及可配置限制或被较新的 MongoDB 版本禁止。",
  "versions": [
    {
      "version": "MongoDB 5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MongoDB 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MongoDB 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Add a hint to force the query to a specific shard without including the shard key",
      "why_fails": "Hints cannot bypass the shard key requirement; the query still targets all shards and fails.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Create an index on the queried field(s) to improve performance",
      "why_fails": "Indexes do not change query routing; the query still lacks the shard key for targeted routing.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Include the shard key field in the query filter. For example, if shard key is { userId: 1 }, change db.collection.find({ name: \"Alice\" }) to db.collection.find({ userId: 123, name: \"Alice\" }).",
      "success_rate": 0.95,
      "how": "Include the shard key field in the query filter. For example, if shard key is { userId: 1 }, change db.collection.find({ name: \"Alice\" }) to db.collection.find({ userId: 123, name: \"Alice\" }).",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the shard key is unknown, retrieve it: sh.status() or use db.collection.getShardDistribution(); then modify the query accordingly.",
      "success_rate": 0.85,
      "how": "If the shard key is unknown, retrieve it: sh.status() or use db.collection.getShardDistribution(); then modify the query accordingly.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Include the shard key field in the query filter. For example, if shard key is { userId: 1 }, change db.collection.find({ name: \"Alice\" }) to db.collection.find({ userId: 123, name: \"Alice\" }).",
    "If the shard key is unknown, retrieve it: sh.status() or use db.collection.getShardDistribution(); then modify the query accordingly."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.mongodb.com/docs/manual/core/sharding-query-isolation/",
  "official_doc_section": null,
  "error_code": "61",
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}