Duplicate Key Error

noun

say it: “DOO-plih-kit KEE AIR-er” /ˈduplɪkət ˈki ˈɛrər/

The error a database gives when a new or changed record would repeat a value that must be unique, such as an id already in use.

What is a duplicate key error in MongoDB?

A duplicate key error (code 11000, shown as E11000) is MongoDB refusing a write because a document with the same _id, or the same value in a field with a unique index, is already in the collection. That document isn't saved.

db.recipes.insertOne({ _id: 4, name: "Chickpea Curry" });
db.recipes.insertOne({ _id: 4, name: "Lentil Soup" });
// MongoServerError: E11000 duplicate key error collection: kitchen.recipes index: _id_ dup key: { _id: 4 }

In a sentence

  • “Running the import twice stopped with a duplicate key error, because recipe 4 was already there.”
  • “The sign-up route turns a duplicate key error on email into a 409 Conflict.”

Level: Apprentice · More MongoDB terms at this level

Updated