1

文档示例:

{
    postId:'232323',
    post:'This is my first post',
    commentsOnPost:[
        {
            commentId:'232323_8888',
            comment:'Congrats',
            repliesOnPost:[
                {
                    replyId:'232323_8888_66666',
                    reply:'Thanks',
                    likesOnReply:['user1','user5','user3'],
                    
                }
            ]
        }
    ]
}

如果用户不存在,我想添加userid,如果存在,同样删除。likesOnReplylikesOnReplyuseridlikesOnReply

我试过这样但不能正常工作

   await collection('post').findOneAndUpdate(
                {
                    postId: postId,
                    'commentsOnPost.commentId': commentId,
                    'commentsOnPost.repliesOnPost.replyId': replyId
                },
                {

                    $push: { 'commentsOnPost.$[].repliesOnPost.$.likes': userid },
                },

            );
4

1 回答 1

1

没有直接的方法可以同时执行拉取或推送单个查询的操作,

有2种方法,

1) 使用 2 个查询查找和更新:

  • 使用arrayFilters更新嵌套数组元素
  • $push插入元素
  • $pull删除元素
var post = await collection('post').findOne({
  posted: postId,
  ommentsOnPost: {
    $elemMatch: {
      commentId: commentId,
      repliesOnPost: {
        $elemMatch: {
          replyId: replyId
          likesOnReply: userid
        }
      }
    }
  }
});

var updateOperator = "$push";
// FOUND USER ID THEN DO REMOVE OPERATION
if (post) updateOperator = "$pull";

// QUERY
await collection('post').updateOne(
  { postId: postId },
  {
    [updateOperator]: {
      "commentsOnPost.$[c].repliesOnPost.$[r].likesOnReply": userid
    }
  },
  {
    arrayFilters: [
      { "c.commentId": commentId },
      { "r.replyId": replyId }
    ]
  }
)

操场

2)从 MongoDB 4.2 开始使用聚合管道更新:

  • $mapcommentsOnPost如果匹配则迭代数组检查条件的循环commentId然后转到下一个进程,否则返回现有对象
  • $mergeObjects将当前对象与更新的字段合并
  • $map迭代数组循环repliesOnPost并检查条件是否replyId匹配然后转到下一个进程,否则返回现有对象
  • 检查条件,likesOnReply然后userid删除使用,$filter否则插入使用$concatArrays
await collection('post').findOneAndUpdate(
  { postId: "232323" },
  [{
    $set: {
      commentsOnPost: {
        $map: {
          input: "$commentsOnPost",
          in: {
            $cond: [
              { $eq: ["$$this.commentId", commentId] },
              {
                $mergeObjects: [
                  "$$this",
                  {
                    repliesOnPost: {
                      $map: {
                        input: "$$this.repliesOnPost",
                        in: {
                          $cond: [
                            { $eq: ["$$this.replyId", replyId] },
                            {
                              $mergeObjects: [
                                "$$this",
                                {
                                  likesOnReply: {
                                    $cond: [
                                      { $in: [userid, "$$this.likesOnReply"] },
                                      {
                                        $filter: {
                                          input: "$$this.likesOnReply",
                                          cond: { $ne: ["$$this", userid] }
                                        }
                                      },
                                      {
                                        $concatArrays: ["$$this.likesOnReply", [userid]]
                                      }
                                    ]
                                  }
                                }
                              ]
                            },
                            "$$this"
                          ]
                        }
                      }
                    }
                  }
                ]
              },
              "$$this"
            ]
          }
        }
      }
    }
  }]
)

Playgorund

于 2021-07-10T08:07:11.183 回答