0

I don't know if this is possible, searched docs but can't find an answer.

I have a scheme where users sign up with Facebook Id and it gives there full name and id.

Later on in the journey users can sign-up to the multiplayer part and choose a unique username defined as such:

//In >> var UserSchema = new mongoose.Schema
multiplayer.username: {type: String, unique: true, required: false}

My question is, when I attempted this with new user it just spits out the error:

New user request: Superman, facebookid: 13131231222
{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: gameserver.users.$multiplayer.username_1  dup key: { : null }]
  name: 'MongoError',
  message: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: gameserver.users.$multiplayer.username_1  dup key: { : null }',

I've tried using default but it doesn't work, and I cannot use random because it's unique and not resettable.

Any ideas?

EDIT

I looked at http://stackoverflow.com/questions/7955040/mongodb-mongoose-unique-if-not-null

and it doesn't work, same error.

Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95
  • It's telling you you're trying to insert a username with value null and that you already have a username with value null. Can you confirm that multiplayer.username of all users you're saving is not null? – Kip Sep 07 '15 at 18:29
  • Some are null and some have chosen. – Oliver Dixon Sep 07 '15 at 18:30
  • Don't use an actual value of `null`, just leave `username` unset (or set to `undefined`). – JohnnyHK Sep 07 '15 at 18:44
  • 3
    I figured it out, Mongoose wasn't updating the scheme on the actual Mongo server. You have to update the scheme manually or drop and recreate. – Oliver Dixon Sep 07 '15 at 18:53

0 Answers0