【发布时间】:2017-05-25 20:36:02
【问题描述】:
每当我尝试插入新记录时,我都会遇到错误,实际上用户名本身并不是我的模型的一部分,但我不确定为什么会出现此错误,请谁能猜出错误。
我的错误,
MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: project1.students.$username_1 dup key: { : null }
我的收藏,
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
}
]
【问题讨论】:
标签: node.js mongodb mongoose mean-stack