微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在双嵌套数组中添加或删除元素?

如何解决如何在双嵌套数组中添加或删除元素?

文档示例:

{
    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 中不存在,我想在 likesOnReply添加 likesOnReply,如果存在,同样从 userid删除 likesOnReply

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

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

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

解决方法

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

有两种方法,

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 }
    ]
  }
)

Playground

2) Update with aggregation pipeline 从 MongoDB 4.2 开始:

  • $map 迭代 commentsOnPost 数组检查条件的循环,如果 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。