过滤内文档,其中包含蒙哥多的对象数组
看看下面我的收藏,
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”下的过滤器评论和回复吗?
请让我知道需要的更多详细信息。
预期输出将是评论列表以及扩展的回复列表,如下所示,
[
{
"_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".
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
根据您的评论,
$oid
代表ObjectId
,并假设一个帖子可以有多个报告的用户,您可以执行以下操作:如您所见 此处
此查询
$match
所需帖子的所有评论,然后将其拆分没有评论reportedUsers
(确定)并通过$facet
步骤对所有reportedUsers
进行分组。然后,它通过$lookup
从回复集合中收集所有回复,并添加一个空的reportedUsers
数组(如果不存在)。下一步是过滤回复,并删除包含之前分组的reportedUsers
列表中所举报用户的所有回复。Edit:
according to your comment that
$oid
representsObjectId
, and assuming a post can have several reported user, you can do something like this:As you can see here
This query is
$match
ing all the comments for the required post, then split it to comments withoutreportedUsers
(ok) and group all thereportedUsers
, via a$facet
step. Then it collects all replies from the replies collection, via$lookup
and adding an emptyreportedUsers
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 ofreportedUsers
grouped before.