[node.js] E11000 duplicate key error index in mongodb mongoose

Following is my user schema in user.js model -

var userSchema = new mongoose.Schema({
    local: {
        name: { type: String },
        email : { type: String, require: true, unique: 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;

This is how I am using it in my controller -

var user = require('./../models/user.js');

This is how I am saving it in the db -

user({'local.email' : req.body.email, 'local.password' : req.body.password}).save(function(err, result){
    if(err)
        res.send(err);
    else {
        console.log(result);
        req.session.user = result;
        res.send({"code":200,"message":"Record inserted successfully"});
    }
});

Error -

{"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1  dup key: { : null }"} 

I checked the db collection and no such duplicate entry exists, let me know what I am doing wrong ?

FYI - req.body.email and req.body.password are fetching values.

I also checked this post but no help STACK LINK

If I removed completely then it inserts the document, otherwise it throws error "Duplicate" error even I have an entry in the local.email

This question is related to node.js mongodb mongoose

The answer is


It's not a big issue but beginner level developers as like me, we things what kind of error is this and finally we weast huge time for solve it.

Actually if you delete the db and create the db once again and after try to create the collection then it's will be work properly.

? mongo
use dbName;
db.dropDatabase();
exit

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

I had the same issue when i tried to modify the schema defined using mangoose. I think the issue is due to the reason that there are some underlying process done when creating a collection like describing the indices which are hidden from the user(at least in my case).So the best solution i found was to drop the entire collection and start again.


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 .

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

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


I have also faced this issue and I solved it. This error shows that email is already present here. So you just need to remove this line from your Model for email attribute.

unique: true

This might be possible that even if it won't work. So just need to delete the collection from your MongoDB and restart your server.


I was facing similar issue, I went about clearing idexes and restarting server and probably almost everything mentioned here according to my understanding and still there was error, turns out the jsonwebtoken secret key that i was passing was undefined, if you are using jsonwebtoken, do check it once


I had the same problem in my project while I social media signup. when the user joins using email first-time work without any issue. when another user signup using email there is an issue due to a null value assigned to the social media id. the reason is only one user should be allowed to insert a null value according to unique true constrain.

Error Schema:

facebookId: {
    type: String,
    index:true
  },

Solution Schema:

  1. Drop the existing index created against facebookId db.users.dropIndex(facebookid_)

2)when we use sparse: true property it allows us to store multiple null values

  facebookId: {
    type: String,
    index:true,
    unique:true,
    sparse:true
  },

I had the same issue. Tried debugging different ways couldn't figure out. I tried dropping the collection and it worked fine after that. Although this is not a good solution if your collection has many documents. But if you are in the early state of development try dropping the collection.

db.users.drop();

This is my relavant experience:

In 'User' schema, I set 'name' as unique key and then ran some execution, which I think had set up the database structure.

Then I changed the unique key as 'username', and no longer passed 'name' value when I saved data to database. So the mongodb may automatically set the 'name' value of new record as null which is duplicate key. I tried the set 'name' key as not unique key {name: {unique: false, type: String}} in 'User' schema in order to override original setting. However, it did not work.

At last, I made my own solution:

Just set a random key value that will not likely be duplicate to 'name' key when you save your data record. Simply Math method '' + Math.random() + Math.random() makes a random string.


Had the same issue I resolved it by removing the unique attribute on the property.

Just find another way to validate or check for unique property values for your schema.


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.


Check indexes of that collection in MongoDB compass and remove those indexes which are not related to it or for try remove all indexes(Not from code but in db).


I faced similar issues , I Just clear the Indexes of particular fields then its works for me . https://docs.mongodb.com/v3.2/reference/method/db.collection.dropIndexes/


for future developers, i recommend, delete the index in INDEX TAB using compass... this NOT DELETE ANY document in your collection Manage Indexes


Change the collection name if it already exists in the database, it will show an error. And if you given any property as unique the same error will occur.


I got this same issue when I had the following configuration in my config/models.js

module.exports.models = {
  connection: 'mongodb',
  migrate: 'alter'
}

Changing migrate from 'alter' to 'safe' fixed it for me.

module.exports.models = {
  connection: 'mongodb',
  migrate: 'safe'
}

mongoDB Atlas has a indexes tab when under their collections tab that allows you to view and delete any index you do not want.


Please clear the collection or Delete the entire collection from MongoDB database and try again later.


Here's how I solved same issue in September 2020. There is a super-fast and easy way from the mongodb atlas (cloud and desktop). Probably it was not that easy before? That is why I feel like I should write this answer in 2020.

First of all, I read above some suggestions of changing the field "unique" on the mongoose schema. If you came up with this error I assume you already changed your schema, but despite of that you got a 500 as your response, and notice this: specifying duplicated KEY!. If the problem was caused by schema configuration and assuming you have configurated a decent middleware to log mongo errors the response would be a 400.

Why is that? In my case was simple, that field on the schema it used to accept only unique values but I just changed it to accept repeated values. Mongodb on the past created an index for that field, and so even after setting "unique" property as "false" on schema, mongodb was still using that index.

Solution? Dropping that index. You can do it in 2 seconds from Mongo Atlas.

Go to your collection. By default you are on "Find" tab. Just select the next one on the right: "Indexes". You will see how there is still an index given to the same field is causing you trouble. Just click the button "Drop Index".

I believe this is a better option than just dropping your entire collection. Basically because this is why it works after dropping the entire collection. Because mongo is not going to set an index if your first entry is using your new schema with "unique: false".

Server response

Cloud Atlas


I had same Issue. Problem was that I have removed one field from model. When I dropped db it fixes


same issue after removing properties from a schema after first building some indexes on saving. removing property from schema leads to an null value for a non existing property, that still had an index. dropping index or starting with a new collection from scratch helps here.

note: the error message will lead you in that case. it has a path, that does not exist anymore. im my case the old path was ...$uuid_1 (this is an index!), but the new one is ....*priv.uuid_1


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

    db.users.drop();

  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:

    db.users.dropIndex(indexName)

I Hope this helped :)


Drop you database, then it will work.

You can perform the following steps to drop your database

step 1 : Go to mongodb installation directory, default dir is "C:\Program Files\MongoDB\Server\4.2\bin"

step 2 : Start mongod.exe directly or using command prompt and minimize it.

step 3 : Start mongo.exe directly or using command prompt and run the following command

i) use yourDatabaseName (use show databases if you don't remember database name)

ii) db.dropDatabase()

This will remove your database. Now you can insert your data, it won't show error, it will automatically add database and collection.


In this situation, log in to Mongo find the index that you are not using anymore (in OP's case 'email'). Then select Drop Index enter image description here


I had a similar problem and I realized that by default mongo only supports one schema per collection. Either store your new schema in a different collection or delete the existing documents with the incompatible schema within the your current collection. Or find a way to have more than one schema per collection.


This is because there is already a collection with the same name with configuration..Just remove the collection from your mongodb through mongo shell and try again.

db.collectionName.remove()

now run your application it should work


Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?

Examples related to mongoose

Querying date field in MongoDB with Mongoose Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true Mongodb: failed to connect to server on first connect Push items into mongo array via mongoose Mongoose: findOneAndUpdate doesn't return updated document mongoError: Topology was destroyed Difference between MongoDB and Mongoose How can you remove all documents from a collection with Mongoose? E11000 duplicate key error index in mongodb mongoose