【发布时间】:2021-01-22 19:19:11
【问题描述】:
知道为什么这个聚合只匹配帖子,但不填充 cmets?
我需要来自 cmets 集合的用户 cmets,但只有空管道返回 cmets
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"
}
}
]
评论对象包含
{
"_id": "5f7de8491af5c0e246d42609",
"isHiden": false,
"text": "...",
"post": "5f7de8491af5c0e246d42605"
}
后模型是
{
"_id": "5f7de8491af5c0e246d42605",
"title": "Corporate Web Coordinator",
"body": "...",
"author": "5f7de8491af5c0e246d42602"
}
}
我想得到如下结果:
{
"_id": "5f7de8491af5c0e246d42605",
"confirm_status": "pending",
"title": "Dynamic Marketing Supervisor",
"body": "...",
"author": "5f7de8491af5c0e246d42604",
"comments": [
{ "_id": "5f7de8491af5c0e246d42609",
"isHiden": false,
"text": "...",
"post": "5f7de8491af5c0e246d42605" }
]
}
我尝试了一切,但没有任何效果......
我会很感激任何帮助
【问题讨论】:
标签: javascript node.js mongodb mongoose mongoose-populate