E11000 duplicate key error index in mongodb mongoo

2018-12-31 22:06发布

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

17条回答
初与友歌
2楼-- · 2018-12-31 22:33

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'
}
查看更多
一个人的天荒地老
3楼-- · 2018-12-31 22:37

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.

查看更多
高级女魔头
4楼-- · 2018-12-31 22:37

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

查看更多
心情的温度
5楼-- · 2018-12-31 22:42

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.

查看更多
公子世无双
6楼-- · 2018-12-31 22:43

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

查看更多
一个人的天荒地老
7楼-- · 2018-12-31 22:44

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.

查看更多
登录 后发表回答