node.js - E11000 duplicate key error index in mongodb mongoose

ID : 10292

viewed : 26

Tags : node.jsmongodbmongoosenode.js

Top 5 Answer for node.js - E11000 duplicate key error index in mongodb mongoose

vote vote


The error message is saying that there's already a record with null as the email. In other words, you already have a user without an email address.

The relevant documentation for this:

If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.

You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.

unique indexes

Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.

In other words, a sparse index is ok with multiple documents all having null values.

sparse indexes

From comments:

Your error says that the key is named mydb.users.$email_1 which makes me suspect that you have an index on both and (The former being old and unused at the moment). Removing a field from a Mongoose model doesn't affect the database. Check with mydb.users.getIndexes() if this is the case and manually remove the unwanted index with mydb.users.dropIndex(<name>).

vote vote


If you are still in your development environment, I would drop the entire db and start over with your new schema.

From the command line

➜ mongo use dbName; db.dropDatabase(); exit 
vote vote


I want to explain the answer/solution to this like I am explaining to a 5-year-old , so everyone can understand .

I have an app.I want people to register with their email,password and phone number . In my MongoDB database , I want to identify people uniquely based on both their phone numbers and email - so this means that both the phone number and the email must be unique for every person.

However , there is a problem : I have realized that everyone has a phonenumber but not everyone has an email address .

Those that don`t have an email address have promised me that they will have an email address by next week. But I want them registered anyway - so I tell them to proceed registering their phonenumbers as they leave the email-input-field empty .

They do so .

My database NEEDS an unique email address field - but I have a lot of people with 'null' as their email address . So I go to my code and tell my database schema to allow empty/null email address fields which I will later fill in with email unique addresses when the people who promised to add their emails to their profiles next week .

So its now a win-win for everyone (but you ;-] ): the people register, I am happy to have their data ...and my database is happy because it is being used nicely ...but what about you ? I am yet to give you the code that made the schema .

Here is the code : NOTE : The sparse property in email , is what tells my database to allow null values which will later be filled with unique values .

var userSchema = new mongoose.Schema({    local: {      name: { type: String },      email : { type: String, require: true, index:true, unique:true,sparse:true},      password: { type: String, require:true },    },    facebook: {      id           : { type: String },      token        : { type: String },      email        : { type: String },      name         : { type: String }    }  });    var User = mongoose.model('User',userSchema);    module.exports = User;

I hope I have explained it nicely . Happy NodeJS coding / hacking!

vote vote


Check collection indexes.

I had that issue due to outdated indexes in collection for fields, which should be stored by different new path.

Mongoose adds index, when you specify field as unique.

vote vote


Well basically this error is saying, that you had a unique index on a particular field for example: "email_address", so mongodb expects unique email address value for each document in the collection.

So let's say, earlier in your schema the unique index was not defined, and then you signed up 2 users with the same email address or with no email address (null value).

Later, you saw that there was a mistake. so you try to correct it by adding a unique index to the schema. But your collection already has duplicates, so the error message says that you can't insert a duplicate value again.

You essentially have three options:

  1. Drop the collection


  2. Find the document which has that value and delete it. Let's say the value was null, you can delete it using:

    db.users.remove({ email_address: null });

  3. Drop the Unique index:


I Hope this helped :)

Top 3 video Explaining node.js - E11000 duplicate key error index in mongodb mongoose