pymongo.errors.OperationFailure: Index build failed: CannotCreateIndex: Values in v:2 index key pattern are not valid
ID: database/mongo-index-build-failed
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7 | active | — | — | — |
Root Cause
An index build operation failed on MongoDB. Common causes: creating a unique index on a field with existing duplicate values, invalid index key pattern, exceeding the index key size limit (8KB for MongoDB 4.2+), running out of disk space during index build, or duplicate index names. Since MongoDB 4.2, index builds are done in the background by default.
genericWorkarounds
-
90% success Deduplicate existing data before creating the unique index
Find duplicates: db.collection.aggregate([{$group:{_id:'$field',count:{$sum:1},ids:{$push:'$_id'}}},{$match:{count:{$gt:1}}}]). Remove duplicates keeping one: iterate results and db.collection.deleteMany({_id:{$in: duplicateIds}}). Then create the unique index: db.collection.createIndex({field:1},{unique:true}). -
82% success Use partial indexes to avoid indexing problematic documents
Create a partial index that only includes documents meeting certain criteria: db.collection.createIndex({field:1},{unique:true,partialFilterExpression:{field:{$exists:true,$ne:null}}}). This excludes documents with null or missing values from the unique constraint, which is often the source of duplicates.
Dead Ends
Common approaches that don't work:
-
Killing the mongod process to abort a stuck index build
80% fail
Killing mongod during an index build leaves the index in an inconsistent state. On restart, MongoDB may need to rebuild the index from scratch, which takes even longer. Use db.currentOp() to monitor progress and db.killOp() to cleanly abort the build if needed.
-
Creating the unique index with dropDups option to automatically remove duplicates
95% fail
The dropDups option was removed in MongoDB 3.0. It no longer exists in modern MongoDB. You must manually deduplicate data before creating the unique index.