【发布时间】:2018-05-13 16:02:24
【问题描述】:
我有以下猫鼬模式:
var mongoose = require('mongoose');
module.exports = mongoose.model('lf', {
_set : {type:Number},
a : {type:String},
b : {type:String},
c : {type:Number},
created_at : {type: Date, default: Date.now},
updated_at : {type: Date, default: Date.now}
},'flr');
在我的代码中,我查询了一些集合并生成了一个包含上述模式的拼接 json 对象。
虽然当我在猫鼬上进行 save() 操作时,我不断得到:
Collection1.findOne({tfC: tfC}).lean().then(FP=> {
if ( FP!== null && FP!== undefined ){
new linkedFixed(FP).save(function(err, result){
console.log(err);
process.exit();
});
}
}).catch(error => {
console.error(error);
});
我收到错误:
{ MongoError: E11000 duplicate key error collection: Da.flr index: _id_ dup key: { : ObjectId('5a0c8b3f10dfe503505fcaec') } at Function.MongoError.create
我没有在我的架构中定义这个 _id 那么为什么猫鼬会在这个索引上生成重复的条目?
我能够理解查找中的 json 对象已经包含一个 _id。然后我删除它,我得到了错误:
MongooseError: document must have an _id before saving
如何在不传递 _id 的情况下将 json 对象保存到集合中? _id 不应该由 mongoose 随机分配且唯一吗?
【问题讨论】:
标签: node.js mongodb mongoose mongoose-schema