title: E11000 duplicate key error index in mongodb mongoose
body: <p>Following is my <code>user</code> schema in <code>user.js</code> model -</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>This is how I am using it in my controller - </p>
  
  <pre><code>var user = require('./../models/user.js');
  </code></pre>
  
  <p>This is how I am saving it in the db -</p>
  
  <pre><code>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"});
      }
  });
  </code></pre>
  
  <p><strong>Error</strong> -</p>
  
  <pre><code>{"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1  dup key: { : null }"} 
  </code></pre>
  
  <p>I checked the db collection and no such duplicate entry exists, let me know what I am doing wrong ?</p>
  
  <p>FYI -  <code>req.body.email</code> and <code>req.body.password</code> are fetching values.</p>
  
  <p>I also checked this post but no help <a href="https://stackoverflow.com/questions/21119928/getting-err-e11000-duplicate-key-error-when-inserting-into-mongo-using-the">STACK LINK</a></p>
  
  <p>If I removed completely then it inserts the document, otherwise it throws error "Duplicate" error even I have an entry in the local.email</p>