Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: E11000 duplicate key error collection with passport

I have the following userSchema

var mongoose = require("mongoose"),
passportLocalMongoose = require("passport-local-mongoose");

var userSchema = new mongoose.Schema({
email: String,
password: String,
userName: String,
fname: String,
lname: String,
userType: Number,
subscribedThreads: [
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Threads"
    }
]
});
// add passport methods
userSchema.plugin(passportLocalMongoose);
// export modules to be used by the file requiring it
module.exports = mongoose.model("Users",userSchema);

The first entry into the collection occurs as it should but the next ones give

{ [MongoError: E11000 duplicate key error collection: KManV3.users 
index: username_1 dup key: { : null }]
name: 'MongoError',
message: 'E11000 duplicate key error collection: KManV3.users index: 
username_1 dup key: { : null }',
driver: true,
code: 11000,
index: 0,
errmsg: 'E11000 duplicate key error collection: KManV3.users index: 
username_1 dup key: { : null }',
getOperation: [Function],
toJSON: [Function],
toString: [Function] 
}

Also, dbname.users.getIndexes() gives:

> db.users.getIndexes()
[
{
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "name" : "_id_",
    "ns" : "KManV3.users"
},
{
    "v" : 1,
    "key" : {
        "password" : 1
    },
    "name" : "password_1",
    "ns" : "KManV3.users",
    "background" : true
},
{
    "v" : 1,
    "unique" : true,
    "key" : {
        "username" : 1
    },
    "name" : "username_1",
    "ns" : "KManV3.users",
    "background" : true
}
]

Apparently every property of schema has been set as unique and I can't add data into collections even if the data is totally different. I'm not sure if it's due to integration of passport.

like image 536
Suhail Gulzar Avatar asked Mar 22 '26 02:03

Suhail Gulzar


1 Answers

Looking at the options for passport-local-mongoose:

usernameField: specifies the field name that holds the username. Defaults to 'username'.

usernameUnique : specifies if the username field should be enforced to be unique by a mongodb index or not. Defaults to true.

Which explains why your collection has a unique index on the (non-existent-in-your-schema) username field.

If you don't actually set this field in documents that you add to the database, MongoDB will use null, and once the first document has been inserted, a subsequent document (also with the field value null for username) will throw an E11000 error.

So first, remove the index on username (and also password, I assume you once marked that field as unique in your schema), and set the proper field name for passport-local-mongoose to use:

userSchema.plugin(passportLocalMongoose, { usernameField : 'userName' });

(or email, if you want that field to be used as unique user identifier)

like image 166
robertklep Avatar answered Mar 23 '26 16:03

robertklep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!