61
database
data_error
ai_generated
true
MongoServerError: 文档中未找到分片键。文档不包含分片键字段'shardKeyField'
MongoServerError: shard key not found in document. Document does not contain shard key field 'shardKeyField'
ID: database/mongodb-shard-key-missing
82%修复率
88%置信度
1证据数
2024-07-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
| MongoDB 8.0 | active | — | — | — |
| pymongo 4.6 | active | — | — | — |
| mongosh 2.1 | active | — | — | — |
根因分析
对分片MongoDB集合的写操作针对一个缺少分片键字段的文档,该字段是路由到正确分片所必需的。
English
A write operation to a sharded MongoDB collection targets a document that is missing the shard key field, which is required for routing to the correct shard.
官方文档
https://www.mongodb.com/docs/manual/core/sharding-shard-key/解决方案
-
Update the document to include the shard key field before the write. Example in pymongo: collection.update_one({'_id': doc_id}, {'$set': {'shardKeyField': value}}, upsert=True) -
If the shard key is based on a hashed field, ensure all documents have that field. Use a migration script to add the field to existing documents: db.collection.updateMany({shardKeyField: {$exists: false}}, {$set: {shardKeyField: default_value}})
无效尝试
常见但无效的做法:
-
Ignore the missing shard key field and retry the write operation without changes.
95% 失败
The operation will fail again because the document still lacks the required shard key field.
-
Remove the shard key index from the collection.
90% 失败
Removing the shard key index can break sharding and cause data distribution issues; it requires re-sharding the collection.