0

When ever i try to insert a new record i am getting the forrlowing error and actually username is itself not part of my model but i am not sure why i am getting this error,can anyone guess the error please.

My error,

MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: project1.students.$username_1  dup key: { : null }

My collection,

 var StudentSchema = new Schema({
  title: { type: String, default: '' },
  first_name: { type: String, default: '' },
  last_name: { type: String, default: '' },
  email: { type: String, default: '' },
  display_name: {
    type: String,
    trim: true
  },
  username: {
    type: String,
    validate: [validateUsername, 'Please enter a valid username: 3+ characters long, non restricted word, characters "_-.", no consecutive dots, does not begin or end with dots, letters a-z and numbers 0-9.'],
    lowercase: true,
    trim: true
  },
 });

My indexes,

 [
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "project1.students"
    },
    {
        "v" : 1,
        "unique" : true,
        "key" : {
            "username" : 1
        },
        "name" : "username_1",
        "ns" : "project1.students",
        "background" : true
    }
]
MMR
  • 2,869
  • 13
  • 56
  • 110

1 Answers1

1

I think your username is indexed and hence whenever you try to insert a doc with the same username this error comes. Indexes should be unique.And if username is not a part of your model can you please tell your models schema.