MongoDB - The phantom E11000 error that drives Mongoose beginners crazy
NicolasBrondinBernard
E11000 duplicate key error collection: db.products index: name_1 dup key: {name: "XXX"}

Article published on 24/11/2020, last updated on 10/08/2026
Last weekend, I was browsing through a support group for beginner developers that I regularly interact with in order to give some advice, when I came across a Mongoose user with the following problem:
My schema doesn't contain a uniqueness index, and yet when I try to insert data, MongoDB returns this error: "E11000 duplicate key error collection: db.products index: name_1 dup key: {name: "XXX"}"
With the schema used by Mongoose attached:
const productSchema = new mongoose.Schema({
name: {type: String, required: true},
image: {type: String, required: true}
});
And surprise, there is indeed no uniqueness index on the schema, and the user even later tried to force the matter by specifying "unique: false" on the "name" attribute, which changed nothing.
The problem
Where does this problem come from? Is it a bug in MongoDB or in Mongoose? The answer is: neither.
It's actually what could be called a side effect.
When using Mongoose while skimming through the docs, one can quickly be led to think that it handles indexes (particularly uniqueness ones) automatically through schemas.
But the truth is that Mongoose only handles the automatic creation of indexes, but does not handle either their modification or their deletion.
This means that if you added "required: true" to one of your model's attributes and restarted your program, a uniqueness index will have been added to your collection in your MongoDB database.
And even if you removed this index from your schema and restarted your program, the index will still be present in the database, thus causing a "duplicate key" error on your attribute if a document with the same attribute value already exists.
This is often what happens, moreover, when you use a schema found online and then modify it.
The solution
To solve this problem, simply use your favorite graphical client (or CLI) to connect to your database, choose the collection causing the issue and list its indexes.
You should find an index with the same name as the one returned in the error (here the index was called name_1) and all you have to do is delete it.
All that's left is to try creating your document again and you won't have any more errors related to unique key duplication!
No spam. Only free content, news, and ever more resources to level up your skills!
Join +1500 developers
No comments yet