MongoDB says duplicate key, but the value is null
E11000 duplicate key error collection: mydb.users index: email_1 dup key: { email: null }Short answer
A unique index in MongoDB stores a missing field as null, and only one null entry is allowed. The second document without that field fails with E11000, even though the key shows null. Drop the index and recreate it as a unique partial index on real values, or stop saving null.
Fix it fast
1Find out which cause you have
Read the index name and the dup key from the error, then list the indexes and count documents where that field is null or missing.
db.users.getIndexes() db.users.countDocuments({ email: null })2Match what you see, then jump to its fix
- 1
dup key: { email: null } and your new documents have no email at allSeveral documents omit the field, and the index is uniqueFix - 2
The index is already unique and sparse, yet the error still shows dup key: { email: null }Documents store an explicit null, so a sparse unique index does not helpFix - 3
The error appears on createIndex or on app start, not on an insertExisting duplicates make the index build failFix
- 1
What does the error look like?
E11000 duplicate key error collection: mydb.users index: email_1 dup key: { email: null }MongoServerError: E11000 duplicate key error
Cause 1 of 3
Several documents omit the field, and the index is unique
1Check
In a unique single-field index, a missing field counts as a null entry, and a second one is a duplicate. The index list shows whether it is unique.
db.users.getIndexes()2Fix
Replace the index with a unique partial index that only covers documents where the field is a real string. Missing and null values are then not indexed and cannot clash.
db.users.dropIndex("email_1")
db.users.createIndex(
{ email: 1 },
{ unique: true, partialFilterExpression: { email: { $type: "string" } } }
)3Verify
Insert two documents without email; both succeed. A repeated real email still fails.
db.users.insertOne({ name: "A" })
db.users.insertOne({ name: "B" })Confirmed by mongodb.com
Cause 2 of 3
Documents store an explicit null, so a sparse unique index does not help
1Check
A sparse index skips documents that lack the field, but a field set to null is present and is indexed. Two documents with email: null then clash. Count them.
db.users.countDocuments({ email: { $type: "null" } })2Fix
Remove the null values so the field is truly missing, and make the application stop writing null. Or use the partial index from the first cause, which skips nulls too.
db.users.updateMany(
{ email: { $type: "null" } },
{ $unset: { email: "" } }
)3Verify
No document keeps a null email.
db.users.countDocuments({ email: { $type: "null" } })Confirmed by mongodb.com
Cause 3 of 3
Existing duplicates make the index build fail
1Check
MongoDB cannot create a unique index when the existing data already violates it, and more than one null entry fails the build. Find the repeated values first.
db.users.aggregate([
{ $group: { _id: "$email", n: { $sum: 1 } } },
{ $match: { n: { $gt: 1 } } }
])2Fix
If the repeated value is null, build the partial index below, which leaves null and missing values out. If it is a real value, clean or merge those documents first, then build the index.
db.users.createIndex(
{ email: 1 },
{ unique: true, partialFilterExpression: { email: { $type: "string" } } }
)3Verify
The index appears in the list.
db.users.getIndexes()Confirmed by mongodb.com
How do I stop it coming back?
- Decide per optional unique field: partial index on string values, or never write null.
- Do not combine sparse and partialFilterExpression on one index; MongoDB does not allow it.
- Test unique indexes in staging with documents that omit the field.
- Build indexes in a migration, not from application start-up.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Databases
Node.js in Docker Compose cannot find mysql2/promise
The app works on the host but fails after docker compose up
mysql2/promise is a path inside the mysql2 package.
2 causes, each with a fixDatabases
Elasticsearch rejects requests with circuit_breaking_exception: Data too large
circuit_breaking_exception with a reason that contains Data too large
A circuit_breaking_exception with 'Data too large' means Elasticsearch refused work to protect a node's JVM heap.
3 causes, each with a fixDatabases
JupyterLab cannot determine its build status without Node.js
Could not determine jupyterlab build status without nodejs
JupyterLab itself runs without Node.js, but source extensions need Node.js to rebuild it.
3 causes, each with a fixDatabases
Mongoose cannot sign in to MongoDB Atlas: bad auth, authentication failed
MongoError: bad auth Authentication failed.
Authentication failed means the server rejected the user in your connection string.
3 causes, each with a fixDatabases
mongodb-memory-server will not start under Jest or in Docker
NoSystemBinaryFoundError
When mongodb-memory-server fails to start, the real reason is hidden behind a generic message.
4 causes, each with a fixDatabases
updateOne() runs without an error but the document does not change
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }
updateOne() reports what it did in its result.
4 causes, each with a fix