How to fix E11000 duplicate key error collection: users index: email_1 dup key: { email: "test@example.com" } in MongoDB

MongoDBINTERMEDIATEHIGHUpdated Dec 22, 2025· 169 views

The E11000 duplicate key error occurs when you try to insert a document with a value that already exists in a unique indexed field. This prevents duplicate emails, usernames, or other unique identifiers from being stored in your MongoDB collection.

Quick fix

Fixes E11000

db.users.getIndexes()
  1. Identify which index is being violated
  2. Check for existing documents with duplicate values
  3. Use upsert instead of insert to prevent duplicates
  4. Remove orphaned indexes from your database
mongodb — error
db.users.getIndexes()
E11000 duplicate key error collection: users index: email_1 dup key: { email: "test@example.com" }

What this error means

MongoDB enforces unique index constraints to maintain data integrity. When a field has a unique index, MongoDB guarantees no two documents can have the same value for that field. The E11000 error is thrown when this constraint is violated—meaning you're attempting to insert or update a document with a value that already exists in the indexed field. In the example error, the "email_1" index on the "users" collection already contains a document with email "test@example.com". When MongoDB detects this conflict, it rejects the operation and returns error code 11000 to indicate a duplicate key violation.

Typical symptoms

  • Application crashes with "MongoError: E11000 duplicate key error" when registering new users
  • Database insert/update operations fail silently in background tasks
  • User registration forms submit successfully but data is not saved
  • Error message appears only after the first document with that value was created
  • Bulk inserts partially fail, some records are inserted while others are rejected

Common causes

  1. Inserting a document with the same value in a uniquely indexed field (most common)
  2. Multiple concurrent insert requests arriving before the first one completes
  3. Orphaned indexes from old schema definitions that no longer exist in your code
  4. Unique indexes created but not properly handled in application logic
  5. Null or undefined values when multiple documents lack a required unique field
  6. Case-sensitive duplicate detection in string fields without case-insensitive indexes

How to fix "E11000 duplicate key error collection: users index: email_1 dup key: { email: "test@example.com" }"

1Identify which index is being violated

The error message shows the index name (e.g., "email_1"). To see all indexes on your collection, run:

javascript
db.users.getIndexes()

This returns all indexes including their options. Look for { "unique": true } in the index definition.

2Check for existing documents with duplicate values

Query your collection to find documents with the problematic value:

javascript
db.users.find({ email: "test@example.com" })

If this returns multiple documents, you have duplicates in the collection. If it returns nothing, the value may already be indexed but you're trying to insert again.

3Use upsert instead of insert to prevent duplicates

Replace direct inserts with upsert operations. Upsert updates the document if it exists, or inserts it if it doesn't:

javascript
db.users.updateOne(
  { email: "test@example.com" },
  { $set: { name: "John", email: "test@example.com" } },
  { upsert: true }
)

With Mongoose:

javascript
await User.findOneAndUpdate(
  { email: "test@example.com" },
  { name: "John", email: "test@example.com" },
  { upsert: true, new: true }
)
4Remove orphaned indexes from your database

If you see indexes that don't correspond to your current schema, drop them:

javascript
db.users.dropIndex("email_1")

Then recreate any indexes you actually need. This is especially important after schema changes or migrations.

5Handle E11000 errors in your application

Catch the error by checking the error code (11000) and provide user-friendly feedback:

javascript
try {
  await User.create({ email: "test@example.com", name: "John" })
} catch (error) {
  if (error.code === 11000) {
    const field = Object.keys(error.keyPattern)[0]
    throw new Error(`A user with this ${field} already exists`)
  }
  throw error
}
6Verify your unique index is working correctly

After fixing the issue, confirm that duplicate values are now properly rejected:

javascript
// This should succeed
db.users.insertOne({ email: "new@example.com", name: "Jane" })

// This should fail with E11000
db.users.insertOne({ email: "new@example.com", name: "John" })

Advanced notes

By default, MongoDB only allows one document to have a null or missing value in a uniquely indexed field. When creating a unique index on existing data with missing values, MongoDB will fail if multiple documents lack the field.

For partial unique indexes (applying only to documents matching a filter), use the partialFilterExpression option:

javascript
db.users.createIndex(
  { email: 1 },
  { unique: true, partialFilterExpression: { status: "active" } }
)

This allows multiple documents to have the same email if they don't match the filter.

In sharded clusters, unique indexes have special behavior: if the indexed field is not the shard key, uniqueness is only enforced within each shard, not globally across the cluster. If you need global uniqueness in a sharded system, the indexed field must be part of your shard key.

Frequently asked questions

What does "E11000" mean?
MongoDB enforces unique index constraints to maintain data integrity. When a field has a unique index, MongoDB guarantees no two documents can have the same value for that field. The E11000 error is thrown when this constraint is violated—meaning you're attempting to insert or update a document with a value that already exists in the indexed field.
How do I fix "E11000"?
The E11000 duplicate key error occurs when you try to insert a document with a value that already exists in a unique indexed field. This prevents duplicate emails, usernames, or other unique identifiers from being stored in your MongoDB collection.
What causes "E11000"?
Inserting a document with the same value in a uniquely indexed field (most common) Multiple concurrent insert requests arriving before the first one completes Orphaned indexes from old schema definitions that no longer exist in your code
Is the "E11000" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading