Have any one idea why this aggregation only matches post, but doesn't populating comments?
I need user comments from comments collection, but only empty pipeline returns comments
Post.aggregate(...)
[
{
"$match": {
"author": ObjectId(...)
}
},
{
"$lookup": {
"from": "comments",
"let": {
"postID": "$post",
"isHiden": "$isHiden"
},
"pipeline": [
{
"$match": {
"$expr": {
"$and": [
{
"$eq": [
"$_id",
"$$postID"
]
},
{
"$eq": [
"$$isHiden",
0
]
}
]
}
}
}
],
"as": "comments"
}
}
]
Comment object contains
{
"_id": "5f7de8491af5c0e246d42609",
"isHiden": false,
"text": "...",
"post": "5f7de8491af5c0e246d42605"
}
Post model is
{
"_id": "5f7de8491af5c0e246d42605",
"title": "Corporate Web Coordinator",
"body": "...",
"author": "5f7de8491af5c0e246d42602"
}
}
I want to get result like:
{
"_id": "5f7de8491af5c0e246d42605",
"confirm_status": "pending",
"title": "Dynamic Marketing Supervisor",
"body": "...",
"author": "5f7de8491af5c0e246d42604",
"comments": [
{ "_id": "5f7de8491af5c0e246d42609",
"isHiden": false,
"text": "...",
"post": "5f7de8491af5c0e246d42605" }
]
}
I tried everything, but nothing works ...
I'll appreciate any help
The problem is in your query,
$post
,isHiden
is not present in post table and you are defining that field in let
, it should be, $lookup: {
from: "comments",
let: { postID: "$_id" },
pipeline: [
{
$match: {
$expr: [
{
$eq: [
"$$postID",
"$post"
]
}
],
isHiden: false
}
}
],
as: "comments"
}