createIndex failed: unique index with duplicates MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

MongoServerError: Index build failed: E11000 duplicate key error collection: db.users index: email_1 dup key

You tried to build a unique index but existing documents already contain duplicate values.

Exact message you may see Index build failed: E11000 duplicate key error collectionMongoServerError: Index build failedE11000 duplicate key error collection: db.users index: email_1 dup key during index buildcreateIndex unique duplicate key existing data
Seen on: MongoDB

Meaning

Find the duplicates with an aggregation ($group by field, $match count > 1), merge or delete them, then rebuild. For optional fields use a partial index. Building on production: do it in a maintenance window or use createIndex with care since index builds take time/resources.

Common causes

  • Duplicate values already in the collection
  • Many documents missing the field (null collisions)
  • Case differences treated as distinct
  • Previous partial failed build
  • Data imported without constraints

⚡ Quick fix

  1. Find duplicates: db.users.aggregate([{ $group: { _id: "$email", n: { $sum: 1 }, ids: { $push: "$_id" } } }, { $match: { n: { $gt: 1 } } }])
  2. Clean the data, then retry createIndex
  3. Use partialFilterExpression for optional fields

Detailed fix by platform

mongosh

  1. db.users.createIndex({ email: 1 }, { unique: true, partialFilterExpression: { email: { $type: "string" } } })

How to diagnose

  1. Duplicates — Which values?
  2. Nulls — Missing field?
  3. Window — Production load?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…BSONObj size is invalid / exceeds maximum document size (16MB) — MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) (MongoDB)BSONObj size is inval…Document failed validation — MongoDB: MongoServerError: Document failed validation (code 121) (MongoDB)Document failed valid…E11000 duplicate key — MongoServerError: E11000 duplicate key error collection: db.users index: email_1 dup key (Node.js)E11000 duplicate keyExecutor error: Sort exceeded memory limit (find().sort) — MongoServerError: Executor error during find command :: caused by :: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting (MongoDB)Executor error: Sort …Index key too large to index — MongoServerError: Index key too large to index (key size exceeds 1024 bytes) / pre-4.2 behaviour (MongoDB)Index key too large t…MongoDB Atlas: connection timed out — IP not whitelisted — MongoServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted (MongoDB)MongoDB Atlas: connec…mongorestore: failed to restore (duplicate key / BSON / version) — mongorestore: Failed: db.coll: error restoring from dump: E11000 duplicate key error / unsupported BSON version (MongoDB)mongorestore: failed …createInd…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.