1

My user model looks like:

var UserSchema = new Schema({
  name: {
      type: String,
      unique: false
  },
  user_url:{
      type: String
  },
  email: {
    type: String,
    lowercase: true
  },
  role: {
    type: String,
    default: 'individual'
  },
  plan: {
    type: String,
    default: 'basic'
  },
  password: String,
  provider: String,
  salt: String,
  facebook: {},
  twitter: {},
  google: {},
  github: {}
});

As you can see, I am trying to allow records with duplicate value in name fields. However when trying to save it throws an error:

Unhandled rejection WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error index: dev.users.$name_1 dup key: { : \"ttp2\" }","op":{"user_url":"ttp2-0","salt":"LY59ooaz+8zZ4z8+dA/7dw==","provider":"local","name":"ttp2","email":"ttp2_1@example.com","password":"wEOc+pejEwfz/s11SWQiMpHxrkh7yLC+q+oa6Tad/QGvH+OFTBMAsVssBRvUKoRjE1dyJkZeh5UvCEJsgXAhNA==","_id":"56b5cc49b6bc872413f6c9fb","__v":0}})

What am I doing wrong?

SuSub
  • 73
  • 1
  • 6
  • Presumably, there is still a unique index on the database. If you remove a unique constraint from the mongoose schema afterwards, that will not reflect on the database. Simply remove that index directly via the mongodb client. – qqilihq Feb 06 '16 at 11:53
  • @qqilihq thanks, that was the issue. I was under the impression that changing the schema would force a re-indexing, guess I was wrong. Please post it as an answer so the I can accept it – SuSub Feb 06 '16 at 12:31
  • Glad it worked. There you go :) – qqilihq Feb 06 '16 at 13:14

1 Answers1

4

Presumably, there is still a unique index on the database. If you remove a unique constraint from the mongoose schema which has been defined before, that will not reflect on the database. Simply remove that index directly via the mongodb client.

You can also update your schema programmatically, by putting something like this into your model.js:

mongooseModel.collection.dropIndex('name_of_your_index');

qqilihq
  • 10,794
  • 7
  • 48
  • 89