Mongodb 完成后mongoose无法保存子文档数组
我正在尝试这样做:Mongodb 完成后mongoose无法保存子文档数组,mongodb,mongoose,Mongodb,Mongoose,我正在尝试这样做: Industry.findOne({_id: id}).exec(function(err, industry){ industry.stats = _.extend(industry.stats, stats); //.......(1) industry.save(function(err) { // nothing is saved }); }); (1)中industry.stats的console.log为 这不
Industry.findOne({_id: id}).exec(function(err, industry){
industry.stats = _.extend(industry.stats, stats); //.......(1)
industry.save(function(err) {
// nothing is saved
});
});
(1)中industry.stats的console.log为
这不起作用,显然industry.stats不是一个对象数组,在两个对象之间遗漏了一个逗号。(我说得对吗?)
如果我像这样直接分配industry.stats
[{stat_id: 545080c8e4e88b1d5a7a6d1b}, {stat_id: 54526ca6b294096d33ca6b36}]
然后它就开始工作了。是否需要先将(1)转换为对象数组?我试过lean()和toObject()等,但没有成功。我错过什么了吗 Lodash
extend
将源对象自身的可枚举属性分配给目标对象。在本例中,源(stats
)的数组属性被复制到industry.stats
。这对阵列不起作用
您必须通过数组函数(push
,pull
,…)更新数组,或者直接设置字段
[{stat_id: 545080c8e4e88b1d5a7a6d1b}, {stat_id: 54526ca6b294096d33ca6b36}]