我将如何使用猫鼬为 Comments 数组中的每个 sub_section 推送一些评论。 请看图
_id:objected('834838959384'),
sections:[
{
title:"title of post",
sub_section:[
{id:objected('5894389'), comments[{author:"jP", text:"lorem" }, ]}
]}
答案 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) { });