简体   繁体   中英

MongoDB Update object inside Multi Nested Array

I am making a project using NodeJS and MongoDB . I am using mongoose as well. I have the following collection:

array1:[
    {
        field1:{type:String,default:null}
        array2:[
            {
                id:{type:String,default:null},
                field1:{type:String,default:null},
                field:{type:String,default:null}
            }
        ]
    }
]

So I want to update "field1" and "field2" inside "array2" using the "id" but I am unable to achieve this with update query. Can you please suggest me on how to achieve this using update query?

Currently to update it I used

document.save(function(error,response){});

Thanks in advance.

我认为最好的方法是将array2设置为一个集合的数组,并为此array2的对象创建一个新的集合。这样做并拥有id即可轻松找到它usign Collection.findById,它还会得到一个更好的性能,因为您无需在查找所需对象之前为array1和array2的所有值读取和分配内存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM