Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.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
Javascript 使用mongoose在嵌套数组中搜索和更新_Javascript_Arrays_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

Javascript 使用mongoose在嵌套数组中搜索和更新

Javascript 使用mongoose在嵌套数组中搜索和更新,javascript,arrays,mongodb,mongoose,mongodb-query,Javascript,Arrays,Mongodb,Mongoose,Mongodb Query,我的模型中文档的简化架构: { ar1: [ { b: { ar2: [{ _id: 1, value: 2 }], }, }, { b: { ar2: [{ _id: 2, value: 2 }], }, }, { b: {

我的模型中文档的简化架构:

{
    ar1: [
        {
            b: {
                ar2: [{ _id: 1, value: 2 }],
            },
        },
        {
            b: {
                ar2: [{ _id: 2, value: 2 }],
            },
        },
        {
            b: {
                ar2: [{ _id: 1, value: 5 }],
            },
        },
    ];
}
现在我想更新ar2中_id等于1的所有元素,以便获得:

{
    ar1: [
        {
            b: {
                ar2: [{ _id: 1, value: 3 }],
            },
        },
        {
            b: {
                ar2: [{ _id: 2, value: 2 }],
            },
        },
        {
            b: {
                ar2: [{ _id: 1, value: 3 }],
            },
        },
    ];
}
以下操作不起作用:

Model.updateMany({
    'ar1.b.ar2._id' : 1
},{
    'ar1.$[].b.ar2.$[].value' : 2
});
有什么建议吗

我的想法是,我希望能够将嵌套数组中服从查询的元素合并并更新它们。

您可以使用combine with来实现这一点:

Model.update(
  {}, 
  { $set: { "ar1.$[].b.ar2.$[el].value": 3 } },  
  { arrayFilters: [ { "el._id": 1 } ] } 
)