MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index

MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index

每当我尝试插入一条新记录时,我都会收到 forrlowing 错误,实际上用户名本身并不是我模型的一部分,但我不确定为什么会收到此错误,有人能猜出错误吗。

我的错误,

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

我的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
  },
 });

我的索引,

 [
    {
        "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
    }
]

我认为您的用户名已编入索引,因此每当您尝试插入具有相同用户名的文档时,都会出现此错误。索引应该是 unique.And 如果用户名不是您模型的一部分,请告诉您的模型架构。