【问题标题】:Mongoose aggregation lookup pipeline doesn't workMongoose 聚合查找管道不起作用
【发布时间】: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


    【解决方案1】:

    问题出在您的查询中,

    • 评论表中存在post字段并且您使用了_id,所以它应该是$post
    • 内部管道字段 isHiden 不存在于 post 表中,您在 let 中定义该字段,应该是,
        $lookup: {
          from: "comments",
          let: { postID: "$_id" },
          pipeline: [
            {
              $match: {
                $expr: [
                  {
                    $eq: [
                      "$$postID",
                      "$post"
                    ]
                  }
                ],
                isHiden: false
              }
            }
          ],
          as: "comments"
        }
    

    Playground

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 2018-12-11
      • 2020-07-16
      • 2021-06-29
      • 2022-11-23
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多