# MongoServerError: 块拆分失败：找不到文档的分片键值

- **ID:** `mongodb/sharded-collection-chunk-split-error`
- **领域:** mongodb
- **类别:** runtime_error
- **错误码:** `60`
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

在块迁移或拆分过程中，均衡器或 mongos 无法确定一个或多个文档的分片键值，通常是由于缺少分片键字段或值为 null。

## 版本兼容性

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

## 解决方案

1. ```
   Find documents with missing or null shard key fields using a query like `db.collection.find({shardKeyField:{$exists:false}})` and update them with a valid value. Example: `db.collection.updateMany({shardKeyField:{$exists:false}},{$set:{shardKeyField:0}})`
   ```
2. ```
   If the shard key is compound, ensure all fields in the shard key exist and are non-null. Use `db.collection.find({$or:[{field1:{$exists:false}},{field2:{$exists:false}}]})` to identify problematic documents.
   ```
3. ```
   As a last resort, export the collection, drop it, and re-import with proper shard key values. Example: `mongodump --collection mycollection; db.mycollection.drop(); sh.shardCollection('mydb.mycollection',{shardKey:1}); mongorestore --collection mycollection`
   ```

## 无效尝试

- **** — This corrupts the sharding metadata and can cause data loss or cluster instability. (20% 失败率)
- **** — The balancer is not the root cause; the documents with missing shard keys remain, so any future split will fail. (15% 失败率)
- **** — If the shard key is compound, adding a single field may not resolve the issue and could violate shard key constraints. (25% 失败率)
