我正在尝试从我的DocumentDB
. 这是数据的结构
{
_id: <objectidgoeshere>,
author: {
userId: 2,
handleName: "VladimirPutin"
}
postBody: "Lorem ipsum dolor amet",
comments: [
{
commentBody: "Another lorem ipsum",
author: {
user_id: 4,
handleName: "JohnDoe"
}
replies: [
{
replyBody: "Reply lorem ipsum",
author: {
user_id: 1,
handleName: "JaneDoe"
}
}
]
},
{
commentBody: "Another lorem ipsum 2",
author: {
user_id: 3,
handleName: "MarkZucc"
}
replies: [
{
replyBody: "Reply lorem ipsum",
author: {
user_id: 4,
handleName: "JohnDoe"
}
}
]
}
]
}
并假设朋友和被阻止的 id 在一个数组中
blocked_ids = [4, 5]
friend_ids = [2, 3]
我想过滤评论,这样它就不会返回被阻止的用户的评论。
我当前的代码看起来像这样
query = [
{"$match": {"author.userId": {"$in": friend_ids}}},
{"$project": {
"postBody": 1,
"comments": {
"$filter": {
"input": "$comments",
"as": "comments",
"cond": {"$nin": ["$$comments.author.user_id", blocked_ids]}
}
}
}
]
不知何故,此查询返回了我朋友的帖子,但评论字段为空。我还需要过滤内部数组replies
。我需要过滤掉被我屏蔽的用户的回复。