title: Mongodb duplicate key error collection dup key: null
body: <p>Im trying to create multiple accounts.</p>
  
  <p>The first account always works, but when im trying to make a new one i get the following error:</p>
  
  <pre><code>BulkWriteError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: db.users.$friends.userid_1  dup key: { : null }
  </code></pre>
  
  <p>The first user is fine, containing friends with an empty array, as i would like it to be.</p>
  
  <p>But the next user is not created.</p>
  
  <p>What do i have to do to fix this error?</p>
  
  <p>The user schema piece of friends in users is the following:</p>
  
  <pre><code>   friends : [
      {
          userid : {type: String, default: '', unique: true },
      }
  ],
  friendRequests: [
      {
          userid : {type: String, default: '', unique: true },
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>i have been looking into <a href="https://docs.mongodb.com/manual/core/index-unique/#unique-index-and-missing-field" rel="nofollow noreferrer">https://docs.mongodb.com/manual/core/index-unique/#unique-index-and-missing-field</a> but still couldnt make it work.</p>
  
  <p>EDIT2:</p>
  
  <p>By default, its not creating any friends or friendrequests.</p>
  
  <p>EDIT3:</p>
  
  <p>full code:</p>
  
  <pre><code>passport.use('local-signup', new LocalStrategy({
              usernameField : 'username',
              passwordField : 'password',
              passReqToCallback : true,
          },
          function(req, username, password, done) {
              process.nextTick(function() {
                  console.log("doing local signup");
                  username = username.toLowerCase();
                  Account.findOne({username :  username }, function(err, user) {
                      var expr = "/admin/";
                      if (err) {
                          return done(err);
                      } else if (user) {
                          return done(null, false, 'That username is already taken.');
                      } else if(username.length &lt; 3 || username.length &gt;= 12) {
                          return done(null, false, 'Username has to be between 3 and 12 characters! :( '  + username);
                      } else if(/^[a-zA-Z0-9- ]*$/.test(username) == false) {
                          return done(null, false, 'You cant have any special characters!');
                      } else if(password.length &lt; 5 || password.length &gt; 15) {
                          return done(null, false, 'Password need to be 5-15 characters long!');
                      } else {
  
                          var newUser            = new Account();
                          newUser.username    = username;
                          newUser.password = newUser.encryptPassword(password);
                          newUser.save(function(err) {
                              if (err)
                                  throw err;
                              return done(null, newUser);
                          });
                      }
                  });
  
              });
  
          }));
  </code></pre>
  
  <p>user model:</p>
  
  <pre><code>var mongoose     = require('mongoose');
  var Schema       = mongoose.Schema;
  var passportLocalMongoose = require('passport-local-mongoose');
  var bcrypt   = require('bcrypt-nodejs');
  
  
  
  var UserSchema   = new Schema({
      username: {type: String, index: { unique: true }},
      password: {type: String},
      salt: { type: String},
      hash: {type: String},
      gender : {type: String, default: 'male'},
      friends : [
          {
              userid : {type: String, default: '', unique: true },
          }
      ],
      friendRequests: [
          {
              userid : {type: String, default: '', unique: true },
          }
      ]
  
  });
  UserSchema.methods.encryptPassword = function(password) {
      return bcrypt.hashSync(password, bcrypt.genSaltSync(10));
  }
  
  UserSchema.methods.validPassword = function(password) {
      return bcrypt.compareSync(password, this.password);
  }
  
  
  
  module.exports = mongoose.model('Users', UserSchema);
  </code></pre>