如何解决MongoDB错误11000,但是键具有唯一的变量:false
我正在使用Node.js,Express和MongoDB开发JavaScript后端应用程序(我遵循了openClassroom教程),并且除了我的madabase的一个集合外,其他所有东西都工作正常:
const frequencySchema = mongoose.Schema({
// Datas of frequencySchema;
});
const reptileSchema = mongoose.Schema({
name: { type: String,required: true,unique: false },imageUrl: { type: String,required: false },group: { type: String,required: true },foodType: { type: [String],frequency: { type: [frequencySchema],});
const lastMealSchema = mongoose.Schema({
// Datas of lastMealSchema;
});
const petSchema = mongoose.Schema({
name: { type: String,owner: { type: String,enabled: { type: Boolean,reptile: { type: reptileSchema,birthday: { type: Date,shade: { type: [Date],lastMeal: { type: [lastMealSchema],calciumFreq: { type: Number,vitaminFreq: { type: Number,required: false }
});
现在,当我沿着路线添加带有这些参数的宠物时:
{
"_id": "0","name": "Godzilla","enabled": true,"birthday": "2015-02-15","shade": [],"calciumFreq": 0,"vitaminFreq": 0,"owner": "{{userId}}","lastMeal": [],"reptile": {
"foodType": [],"_id": "5f5695c255b55e001782f8ad","name": "Gecko Leopard","group": "Lezard","frequency": []
}
}
它工作正常,然后如果我第二次这样做,它将给我MongoDB错误11000:
"error": {
"driver": true,"name": "MongoError","index": 0,"code": 11000,"keyPattern": {
"reptile.name": 1
},"keyValue": {
"reptile.name": "Gecko Leopard"
}
}
但是我将这些值参数设置为false。
解决方法
您正在尝试提供参考(我想填充一下)。不是type: reptileSchema
,而是{ type: Schema.Types.ObjectId,ref: reptileSchema }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。