InletDownload

MongoDB error 11000

E11000 duplicate key error collection

A unique index already holds the value your insert or update tried to write. The message names the collection, the index and the duplicate key; find the document that has it, then upsert, change the value or remove duplicates.

E11000 duplicate key error collection: seo_mongo.users index: email_1 dup key: { email: "ada@example.com" }

Tested on MongoDB 8.0.32 (mongosh 2.12.0) · Updated 9 October 2026

What it means

MongoDB refused the write because a unique index already has an entry with the same key. An ordinary collection always has a unique index on _id, named _id_; any others were created with unique: true. The server returns code 11000 (DuplicateKey), and the message has three parts:

  • collection: seo_mongo.users: the database and collection.
  • index: email_1: the index that refused it. Default index names are the field and direction, so email_1 is an ascending index on email.
  • dup key: { email: "ada@example.com" }: the value that’s already there.

Drivers also give the key as fields on the error (keyPattern and keyValue in the Node.js driver), so code can tell which index failed without parsing the text.

Common causes

  1. The document already exists. An import or seed ran twice, a retry repeated an insert that had worked, or a form was submitted twice.
  2. An _id you supplied is taken, often when copying documents between collections or databases.
  3. Documents without the field. A unique index stores a missing field as null, so the second document without an email fails with dup key: { email: null }.
  4. An update or upsert sets a value another document has.
  5. Creating a unique index over existing duplicates. The index build fails, and the message starts with Index build failed: and ends with the E11000 text.
  6. A case-insensitive index. With a collation of strength 1 or 2, Ada@Example.com and ada@example.com are the same key; the message shows the collation and a CollationKey(…) instead of the value.

How to fix it

Find the document that has the key

Use the dup key from the message as the filter:

db.users.findOne({ email: "ada@example.com" })

If it’s the document you meant to write, update it instead of inserting a second one.

Insert or update in one call

An upsert updates the matching document, or inserts one if none matches:

db.users.updateOne(
  { email: "ada@example.com" },
  { $set: { name: "Ada L." } },
  { upsert: true }
)

Filter on the unique field: MongoDB’s documentation recommends a unique index on the upsert’s filter fields so that concurrent upserts can’t create two documents.

Keep going past duplicates in bulk inserts

insertMany stops at the first error by default, so documents after the duplicate aren’t inserted. With { ordered: false }, MongoDB inserts every document it can and reports the duplicates in one error at the end.

Allow documents without the field

Make the index cover only documents that have the field, with a partial index:

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

Documents without email are left out of the index, so any number of them can exist. A sparse: true unique index has a similar effect.

Remove duplicates before creating a unique index

List them:

db.people.aggregate([
  { $group: { _id: "$email", n: { $sum: 1 }, ids: { $push: "$_id" } } },
  { $match: { n: { $gt: 1 } } }
])

Decide which document of each group to keep, delete the others by _id, then create the index.

Reproduce it

MongoDB 8.0.32 in Docker, mongosh 2.12.0, in a scratch database:

db.users.createIndex({ email: 1 }, { unique: true })
db.users.insertOne({ _id: 1, email: "ada@example.com", name: "Ada" })
db.users.insertOne({ email: "ada@example.com", name: "Ada again" })
MongoServerError: E11000 duplicate key error collection: seo_mongo.users index: email_1 dup key: { email: "ada@example.com" }

Reusing _id: 1 names the _id_ index instead:

MongoServerError: E11000 duplicate key error collection: seo_mongo.users index: _id_ dup key: { _id: 1 }

Two documents without email, inserted with insertMany:

MongoBulkWriteError: E11000 duplicate key error collection: seo_mongo.users index: email_1 dup key: { email: null }

insertMany with c@example.com, ada@example.com and d@example.com inserted only c; with { ordered: false }, the documents before and after the duplicate were both inserted. Creating a unique index over two copies of x@example.com:

MongoServerError: Index build failed: 510bc6c3-…: Collection seo_mongo.people ( d98ce992-… ) :: caused by :: E11000 duplicate key error collection: seo_mongo.people index: email_1 dup key: { email: "x@example.com" }

With the partial index above, two documents without email went in, and the upsert matched and updated the existing document (matchedCount: 1, upsertedCount: 0).

In Inlet

Inlet shows MongoDB collections as tables, with each document in the inspector, and shows the server’s message when a write is refused, index and key included. Queries use mongosh syntax, so db.users.find({ email: "ada@example.com" }) finds the document that already has the key.

Related

Sources