过滤内文档,其中包含蒙哥多的对象数组

发布于 2025-01-20 08:35:41 字数 2406 浏览 0 评论 0原文

看看下面我的收藏,

db={
  "replies": [
    {
      "_id": {
        "$oid": "60c4814d09488145b72beda9"
      },
      "post": [
        {
          "$oid": "5fc67eb5111f570dc3eb7087"
        }
      ],
      "likes": [],
      "text": "Reply not reported",
      "comment": {
        "$oid": "60c4813f09488145b72beda8"
      },
      "__v": 0
    },
    {
      "_id": {
        "$oid": "60c4815609488145b72bedaa"
      },
      "post": [
        {
          "$oid": "5fc67eb5111f570dc3eb7087"
        }
      ],
      "likes": [],
      "text": "Reply reported",
      "comment": {
        "$oid": "60c4813f09488145b72beda8"
      },
      "__v": 0,
      "reportCount": 1,
      "reportedUsers": [
        {
          "$oid": "6252fe50a5cbd65064d4aab8"
        }
      ]
    }
  ],
  "comments": [
    {
      "_id": {
        "$oid": "5fca1877111f570dc3eb7088"
      },
      "replies": [],
      "text": "Reported comment",
      "post": {
        "$oid": "5fc67eb5111f570dc3eb7087"
      },
      "reportCount": 1,
      "reportedUsers": [
        {
          "$oid": "6252fe50a5cbd65064d4aab8"
        }
      ],
      "__v": 0
    },
    {
      "_id": {
        "$oid": "60c4813f09488145b72beda8"
      },
      "replies": [
        {
          "$oid": "60c4814d09488145b72beda9"
        },
        {
          "$oid": "60c4815609488145b72bedaa"
        }
      ],
      "text": "Comment not reported",
      "post": {
        "$oid": "5fc67eb5111f570dc3eb7087"
      },
      "__v": 0
    }
  ]
}

我正在尝试从帖子(对象 ID - 5fc67eb5111f570dc3eb7087)获取评论和回复。假设我阻止了 ID 为“6252fe50a5cbd65064d4aab8”的用户。在这种情况下,结果不应包含 id 为“5fca1877111f570dc3eb7088”的第一条评论reportedUsers 包含上述 id,并且带有 id 的回复也需要过滤掉,因为它也被报告。 这两条评论都来自帖子“5fc67eb5111f570dc3eb7087”。如果报告的用户包含用户 ID“6252fe50a5cbd65064d4aab8”,有人可以帮助汇总查询帖子“5fc67eb5111f570dc3eb7087”下的过滤器评论和回复吗?

Mongodb Playground url

请让我知道需要的更多详细信息。

预期输出将是评论列表以及扩展的回复列表,如下所示,

[
  {
    "_id": "60c4813f09488145b72beda8",
    "replies": [
      {
        "_id": "60c4814d09488145b72beda9",
        "post": "5fc67eb5111f570dc3eb7087",
        "likes": [],
        "text": "Reply not reported",
        "comment": "60c4813f09488145b72beda8"
      }
    ],
    "text": "Comment not reported",
    "post": "5fc67eb5111f570dc3eb7087"
  }
]

Have a look at my collections below,

db={
  "replies": [
    {
      "_id": {
        "$oid": "60c4814d09488145b72beda9"
      },
      "post": [
        {
          "$oid": "5fc67eb5111f570dc3eb7087"
        }
      ],
      "likes": [],
      "text": "Reply not reported",
      "comment": {
        "$oid": "60c4813f09488145b72beda8"
      },
      "__v": 0
    },
    {
      "_id": {
        "$oid": "60c4815609488145b72bedaa"
      },
      "post": [
        {
          "$oid": "5fc67eb5111f570dc3eb7087"
        }
      ],
      "likes": [],
      "text": "Reply reported",
      "comment": {
        "$oid": "60c4813f09488145b72beda8"
      },
      "__v": 0,
      "reportCount": 1,
      "reportedUsers": [
        {
          "$oid": "6252fe50a5cbd65064d4aab8"
        }
      ]
    }
  ],
  "comments": [
    {
      "_id": {
        "$oid": "5fca1877111f570dc3eb7088"
      },
      "replies": [],
      "text": "Reported comment",
      "post": {
        "$oid": "5fc67eb5111f570dc3eb7087"
      },
      "reportCount": 1,
      "reportedUsers": [
        {
          "$oid": "6252fe50a5cbd65064d4aab8"
        }
      ],
      "__v": 0
    },
    {
      "_id": {
        "$oid": "60c4813f09488145b72beda8"
      },
      "replies": [
        {
          "$oid": "60c4814d09488145b72beda9"
        },
        {
          "$oid": "60c4815609488145b72bedaa"
        }
      ],
      "text": "Comment not reported",
      "post": {
        "$oid": "5fc67eb5111f570dc3eb7087"
      },
      "__v": 0
    }
  ]
}

I am trying to get comments and replies from post (Object id - 5fc67eb5111f570dc3eb7087). Assume I blocked user with id "6252fe50a5cbd65064d4aab8". In this case the results should not contain the first comment with id "5fca1877111f570dc3eb7088" reportedUsers contains the above mentioned id and the reply with id also need to filter out since it is also reported.
Both comments are comes under post "5fc67eb5111f570dc3eb7087". Can somebody help with the aggregate query for filter comments and replies under post "5fc67eb5111f570dc3eb7087" if the reported users includes user id "6252fe50a5cbd65064d4aab8".

Mongodb playground url

Please let me know any more details needed.

Expected output will be list of comments with expanded list of replies like below,

[
  {
    "_id": "60c4813f09488145b72beda8",
    "replies": [
      {
        "_id": "60c4814d09488145b72beda9",
        "post": "5fc67eb5111f570dc3eb7087",
        "likes": [],
        "text": "Reply not reported",
        "comment": "60c4813f09488145b72beda8"
      }
    ],
    "text": "Comment not reported",
    "post": "5fc67eb5111f570dc3eb7087"
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2025-01-27 08:35:41

编辑:

根据您的评论, $oid 代表 ObjectId,并假设一个帖子可以有多个报告的用户,您可以执行以下操作:

db.comments.aggregate([
  {
    $match: { "post": ObjectId("5fc67eb5111f570dc3eb7087")}
  },
  {
    $facet: {
      reportedUsers: [
        {
          $match: {reportedUsers: { $ne: []} }
        },
        {
          $project: {
            _id: 0,
            reportedUsers: 1
          }
        },
        { $unwind: "$reportedUsers" },
        {
          $group: {
            _id: 0,
            rep: { $addToSet: "$reportedUsers"  }
          }
        }
      ],
      ok: [ {$match: {reportedUsers: null}} ]
    }
  },
  {$unwind: "$ok"},
  {
    $lookup: {
      from: "replies",
      localField: "ok.replies",
      foreignField: "_id",
      let: {
        reportedUsers: "$reportedUsers"
      },
      pipeline: [
        {
          $addFields: {
            reportedUsers: {
              "$ifNull": [
                "$reportedUsers",
                []
              ]
            }
          }
        }
      ],
      as: "ok.replies"
    }
  },
  {
    $project: {
      ok: 1,
      reportedUsers: {
        "$arrayElemAt": [
          "$reportedUsers",
          0
        ]
      }
    }
  },
  {
    $addFields: {
      "replies": {
        $filter: {
          input: "$ok.replies",
          as: "item",
          cond: {
            $eq: [
              {
                $size: {
                  "$setIntersection": [
                    "$item.reportedUsers",
                    "$reportedUsers.rep"
                  ]
                }
              },
              0
            ]
          }
        }
      },
      reportedUsers: 0
    }
  },
  {
    $project: {
      _id: "$ok._id",
      post: "$ok.post",
      replies: 1,
      text: "$ok.text"
    }
  }
])

如您所见 此处

此查询$match所需帖子的所有评论,然后将其拆分没有评论reportedUsers(确定)并通过 $facet 步骤对所有 reportedUsers 进行分组。然后,它通过 $lookup 从回复集合中收集所有回复,并添加一个空的 reportedUsers 数组(如果不存在)。下一步是过滤回复,并删除包含之前分组的 reportedUsers 列表中所举报用户的所有回复。

Edit:

according to your comment that $oid represents ObjectId, and assuming a post can have several reported user, you can do something like this:

db.comments.aggregate([
  {
    $match: { "post": ObjectId("5fc67eb5111f570dc3eb7087")}
  },
  {
    $facet: {
      reportedUsers: [
        {
          $match: {reportedUsers: { $ne: []} }
        },
        {
          $project: {
            _id: 0,
            reportedUsers: 1
          }
        },
        { $unwind: "$reportedUsers" },
        {
          $group: {
            _id: 0,
            rep: { $addToSet: "$reportedUsers"  }
          }
        }
      ],
      ok: [ {$match: {reportedUsers: null}} ]
    }
  },
  {$unwind: "$ok"},
  {
    $lookup: {
      from: "replies",
      localField: "ok.replies",
      foreignField: "_id",
      let: {
        reportedUsers: "$reportedUsers"
      },
      pipeline: [
        {
          $addFields: {
            reportedUsers: {
              "$ifNull": [
                "$reportedUsers",
                []
              ]
            }
          }
        }
      ],
      as: "ok.replies"
    }
  },
  {
    $project: {
      ok: 1,
      reportedUsers: {
        "$arrayElemAt": [
          "$reportedUsers",
          0
        ]
      }
    }
  },
  {
    $addFields: {
      "replies": {
        $filter: {
          input: "$ok.replies",
          as: "item",
          cond: {
            $eq: [
              {
                $size: {
                  "$setIntersection": [
                    "$item.reportedUsers",
                    "$reportedUsers.rep"
                  ]
                }
              },
              0
            ]
          }
        }
      },
      reportedUsers: 0
    }
  },
  {
    $project: {
      _id: "$ok._id",
      post: "$ok.post",
      replies: 1,
      text: "$ok.text"
    }
  }
])

As you can see here

This query is $matching all the comments for the required post, then split it to comments without reportedUsers (ok) and group all the reportedUsers, via a $facet step. Then it collects all replies from the replies collection, via $lookup and adding an empty reportedUsers array if it does not exists. Next step is filter the replies and remove all replies that has a reported user that was in our list of reportedUsers grouped before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文