{"id":688,"hash":"432042415c331c0054cfe9f5c03cf5e2d3632348eff49c5f51e3e7013fdea4f1","pattern":"E11000 duplicate key error index dup key { : null }","full_message":"Following is my user schema in user.js model -\n\nvar userSchema = new mongoose.Schema({\n    local: {\n        name: { type: String },\n        email : { type: String, require: true, unique: true },\n        password: { type: String, require:true },\n    },\n    facebook: {\n        id           : { type: String },\n        token        : { type: String },\n        email        : { type: String },\n        name         : { type: String }\n    }\n});\n\nvar User = mongoose.model('User',userSchema);\n\nmodule.exports = User;\n\nThis is how I am using it in my controller -\n\nvar user = require('./../models/user.js');\n\nThis is how I am saving it in the db -\n\nuser({'local.email' : req.body.email, 'local.password' : req.body.password}).save(function(err, result){\n    if(err)\n        res.send(err);\n    else {\n        console.log(result);\n        req.session.user = result;\n        res.send({\"code\":200,\"message\":\"Record inserted successfully\"});\n    }\n});\n\nThe error thrown is:\n\n{\"name\":\"MongoError\",\"code\":11000,\"err\":\"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1  dup key: { : null }\"}\n\nI checked the db collection and no such duplicate entry exists.\n\nFYI -  req.body.email and req.body.password are fetching values.\n\nI also checked Getting \"err\" : \"E11000 duplicate key error when inserting into mongo using the Java driver but it didn't help.\n\nIf I removed completely then it inserts the document, otherwise it throws error \"Duplicate\" error even I have an entry in the local.email. What is happening?","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"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.\n\nThe relevant documentation for this:\n\nIf 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.\n\nYou can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.\n\nunique indexes\n\nSparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.\n\nIn other words, a sparse index is ok with multiple documents all having null values.\n\nsparse indexes\n\nFrom comments:\n\nYour error says that the key is named mydb.users.$email_1 which makes me suspect that you have an index on both users.email and users.local.email (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>).","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/24430220/e11000-duplicate-key-error-index-dup-key-null","votes":376,"created_at":"2026-04-19T04:51:30.400837+00:00","updated_at":"2026-04-19T04:51:30.400837+00:00"}