# MongoServerError：ShardKeyNotFound：对分片集合 test.orders 的查询未包含分片键

- **ID:** `mongodb/sharded-cluster-query-routing-failed`
- **领域:** mongodb
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

对分片集合的查询被路由到所有分片（分散-收集），因为查询未包含分片键，增加了延迟和负载。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## 解决方案

1. ```
   Modify the query to include the shard key in the filter to enable targeted routing (e.g., add shard key field to the query).
   ```
2. ```
   Use a hashed shard key to distribute data evenly and support targeted queries on the hashed value.
   ```
3. ```
   If the query cannot include the shard key, use aggregation pipelines with $merge to pre-filter data on each shard.
   ```

## 无效尝试

- **** — Adding an index on the query fields does not bypass the need for the shard key in targeted queries. (50% 失败率)
- **** — Disabling the balancer does not fix the query routing; it only affects chunk migration. (50% 失败率)
- **** — Changing the shard key after data insertion requires re-sharding, which is a disruptive operation. (50% 失败率)
