Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 完成后mongoose无法保存子文档数组_Mongodb_Mongoose - Fatal编程技术网

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}]