E11000: E11000 duplicate key error collection
MongoDBWhat does it mean?
An insert or update tried to write a value that already exists in the collection into field(s) protected by a unique index. MongoDB rejects the duplicate key.
Why does it happen?
An _id collision, writing the same value a second time to a field with a unique index, a race condition during upsert, or null values colliding for more than one document in a unique index (non-sparse index).
How to fix it
Check whether the document exists before writing, or use an upsert: db.collection.updateOne(filter, {$set:{...}}, {upsert:true}). Look at the index name and the dup key value in the error message. If uniqueness is not actually required, drop the index; if nulls are the problem, use a partial/sparse index. In the application, catch the error and retry or report it to the user.