60
mongodb
runtime_error
ai_generated
partial
MongoServerError: 块拆分失败:找不到文档的分片键值
MongoServerError: chunk split failed: could not find the shard key value for document
ID: mongodb/sharded-collection-chunk-split-error
78%修复率
83%置信度
1证据数
2023-11-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| mongodb-4.2 | active | — | — | — |
| mongodb-4.4 | active | — | — | — |
| mongodb-5.0 | active | — | — | — |
| mongodb-6.0 | active | — | — | — |
| mongodb-7.0 | active | — | — | — |
根因分析
在块迁移或拆分过程中,均衡器或 mongos 无法确定一个或多个文档的分片键值,通常是由于缺少分片键字段或值为 null。
English
During chunk migration or splitting, the balancer or mongos cannot determine the shard key value for one or more documents, often due to missing shard key fields or null values.
官方文档
https://www.mongodb.com/docs/manual/core/sharding-data-partitioning/解决方案
-
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}})` -
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. -
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`
无效尝试
常见但无效的做法:
-
20% 失败
This corrupts the sharding metadata and can cause data loss or cluster instability.
-
15% 失败
The balancer is not the root cause; the documents with missing shard keys remain, so any future split will fail.
-
25% 失败
If the shard key is compound, adding a single field may not resolve the issue and could violate shard key constraints.