猫鼬聚合管道查找和嵌套对象

时间:2020-04-03 20:58:48

标签: node.js mongodb mongoose aggregate

我想和你分享我的问题。

所以我有3个需要在查询中访问的实体:

评估:

    [
        {
            _id: 1,
            questionary: 1,
            subject: 1
        },
        {
            _id: 1,
            questionary: 1,
            subject: 2
        },
    ] 

用户

[
    {
        _id: 1
        name: "John Doe",
        photo: "photo1.jpg"
    },
        {
        _id: 2
        name: "Paul Smith",
        photo: "photo2.jpg"
    },
]

疑问句

[
    {
        _id: 1,
        title: "questionary 1",
        "date": "2020-02-08T00:00:00.000Z" 
    },
    {
        _id: 2,
        title: "questionary 2",
        "date": "2020-02-09T00:00:00.000Z" 
    }
]

所以我的目标是获取这样的数据:其中的问题清单,与问题相关的评估清单以及评估中我需要一个用户对象。像这样:

    [
        {
            "_id": "1",
            "title": "questionary 1",
            "evaluations": [
                {
                    "_id": "1",
                    "date": "2020-04-05T18:53:46.948Z"
                    "user": {
                        _id: 1,
                        "name": "John Doe",
                        "photo": "photo1.jpg"
                    }
                },
                {
                    "_id": "2",
                    "date": "2020-04-06T18:53:46.948Z",
                    "user": {
                        _id: 1,
                        "name": "John Doe",
                        "photo": "photo1.jpg"
                    }
                }
            ]
        }
    ]

我的查询是:

return await Questionary.aggregate([{
      $lookup: {
        from: "evaluation", 
        localField: "_id",  
        foreignField: "questionary", 
        as: "evaluations",
      }
    },
    {
      $lookup: {
        from: "user",
        localField: "evaluations.user",
        foreignField: "_id",
        as: "user",
      }
    },
    { 
        $project: {
            _id: 1,
            title: 1,
            status: 1,
            evaluations: {
                _id: 1,
                date: 1,
                user: "$user"
            }
        },
    },
]);

我的结果是:

[
    {
        "_id": "1",
        "title": "questionary 1",
        "evaluations": [
            {
                "_id": "1",
                "date": "2020-04-05T18:53:46.948Z"
                "user": [
                    {
                        _id: 1,
                        "name": "John Doe",
                        "photo": "photo1.jpg"
                    },
                    {
                        _id: 2,
                        "name": "Paul Smith",
                        "photo": "photo2.jpg"
                    }
                ]
            },
            {
                "_id": "2",
                "date": "2020-04-06T18:53:46.948Z",
                "user": [
                    {
                        _id: 1,
                        "name": "John Doe",
                        "photo": "photo1.jpg"
                    },
                    {
                        _id: 2,
                        "name": "Paul Smith",
                        "photo": "photo2.jpg"
                    }
                ] 
            }
        ]
    }
]

我的评估用户正在合并,但这不是我想要的,我只希望评估内部用户的内部信息。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您应该能够通过使用https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries中所述的不相关子查询来使用嵌套的$lookup

在您的情况下,将是:

return await Questionary.aggregate([{
      $lookup: {
        from: "evaluation",
        let: {questId: "$_id"},
        pipeline: [{
          $match: {
            $expr: {
              $eq: ["$$questId", "$questionary"]
            }
          },
        }, {
          $lookup: {
            from: "user",
            localField: "subject",
            foreignField: "_id",
            as: "user",
          }
        }],
        as: "evaluations"
      }
    }
]);