猫鼬推对象的嵌套子文档数组

时间:2021-03-16 14:45:21

标签: node.js mongodb mongoose

我将如何使用猫鼬为 Comments 数组中的每个 sub_section 推送一些评论。 请看图

_id:objected('834838959384'),
sections:[
  {
    title:"title of post", 
    sub_section:[
    {id:objected('5894389'), comments[{author:"jP", text:"lorem" }, ]}
    

]}

1 个答案:

答案 0 :(得分:0)

根据您提供的信息,您可以执行以下操作:

UserModel.findOneAndUpdate({ _id: ObjectId("834838959384") }, {
  $push: { "sections.$[sec].sub_section.$[subsec].comments": commentToAdd }
}, {
  arrayFilters: [ { "sec.title": "title of post" }, { "subsec.id": ObjectId("5894389") } ]
}, function (err, user) {  });