# MongoServerError: chunk split failed: could not find the shard key value for document

- **ID:** `mongodb/sharded-collection-chunk-split-error`
- **Domain:** mongodb
- **Category:** runtime_error
- **Error Code:** `60`
- **Verification:** ai_generated
- **Fix Rate:** 78%

## Root Cause

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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| mongodb-4.2 | active | — | — |
| mongodb-4.4 | active | — | — |
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## Workarounds

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}})`** (85% success)
   ```
   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.** (80% success)
   ```
   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`** (70% success)
   ```
   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`
   ```

## Dead Ends

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