如何在猫鼬中使用聚合和$ lookup进行过滤?

时间:2019-10-13 16:13:03

标签: node.js mongodb express mongoose mongodb-query

我有一个针对患者的问题列表,每个患者在mongoDB中都有一个ID(患者ID)。在每个问题中,我都有很多评论(我按评论类型将每个评论分开:对于患者的后续笔记评论,类型=“询问”,对于医生评论,类型=“答案”) 我想返回一个注释数组,但是按类型=“ ask”进行过滤,我该怎么办。目前,我仅按阵列返回所有评论,包括患者和医生。我正在使用这样的聚合和查找。 如何按类型过滤?请帮我?谢谢

findQuestionByPatientId(req, res, next){
        Questions.aggregate([
            {
                $match: {
                    $and:[{"patientId":ObjectId(req.params.patientId)}]
                }
            },
            {

              $lookup:
                {
                  from: "comments", // at present It's list all comments include type ="ask" and type = "answer". I want to filter only comment with type ="ask"
                  localField: "_id",
                  foreignField: "questionId",
                  as: "comments"
                }
           }
        ]).then(data => {
            res.send({"data": data, "resultCode": 1, "message " : ""});
        });

    }

下面是示例数据,列表中有2个问题:

{
    "data": [
        {
            "_id": "5d8385844ad1d5001058f220",
            "patientId": "5c80930d447df7735138693e",
            "title": "I have a stomachache",
            "askFor": "myself",
            "age": 28,
            "gender": "",
            "askContent": "I have a stomachache, please give me a prescription",
            "attachment": "5d8385844ad1d5001058f21f",
            "askToDoctor": "5d1cd947231ceb95b8838c1b",
            "createdBy": "5c80930d447df7735138693e",
            "askStatus": "Awaiting Reply",
            "approvedBy": "",
            "approved": false,
            "createdAt": "2019-09-19T13:41:24.874Z",
            "updatedAt": "2019-09-19T13:41:24.874Z",
            "__v": 0,
            "comments": [
                {
                    "_id": "5da0039d9a3d852cf08aa89d",
                    "content": "I sent my image in attached file",
                    "type": "ask",
                    "questionId": "5d8385844ad1d5001058f220",
                    "approved": false,
                    "createdBy": "5d1cd947231ceb95b8838c1b",
                    "attachedFile": null,
                    "approvedBy": "5d1cd947231ceb95b8838c1b",
                    "createdAt": "2019-10-11T04:22:53.538Z",
                    "updatedAt": "2019-10-11T04:22:53.538Z",
                    "__v": 0
                },
                {
                    "_id": "5da003a39a3d852cf08aa89f",
                    "content": "Doctor, plz give me a detail guide",
                    "type": "ask",
                    "questionId": "5d8385844ad1d5001058f220",
                    "approved": false,
                    "createdBy": "5d1cd947231ceb95b8838c1b",
                    "attachedFile": null,
                    "approvedBy": "5d1cd947231ceb95b8838c1b",
                    "createdAt": "2019-10-11T04:22:59.105Z",
                    "updatedAt": "2019-10-11T04:22:59.105Z",
                    "__v": 0
                },
                {
                    "_id": "5da003aa9a3d852cf08aa8a1",
                    "content": "I sent you the guide to take medicine in attached",
                    "type": "reply",
                    "questionId": "5d8385844ad1d5001058f220",
                    "approved": false,
                    "createdBy": "5d1cd947231ceb95b8838c1b",
                    "attachedFile": null,
                    "approvedBy": "5d1cd947231ceb95b8838c1b",
                    "createdAt": "2019-10-11T04:23:06.896Z",
                    "updatedAt": "2019-10-11T04:23:06.896Z",
                    "__v": 0
                }
            ]
        },
        {
            "_id": "5d8b2e9406e021904a797838",
            "patientId": "5c80930d447df7735138693e",
            "title": "I got a flu",
            "askFor": "Myseft",
            "age": 34,
            "gender": "",
            "askContent": "I got a flu, please give me a medicine",
            "attachment": "5d8b2e9406e021904a797837",
            "askToDoctor": "5d1cd947231ceb95b8838c1b",
            "createdBy": "5c80930d447df7735138693e",
            "askStatus": "Awaiting Reply",
            "approvedBy": "",
            "approved": false,
            "createdAt": "2019-09-25T09:08:36.891Z",
            "updatedAt": "2019-09-25T09:08:36.891Z",
            "__v": 0,
            "comments": []
        }
    ],
    "resultCode": 1,
    "message ": ""
}

1 个答案:

答案 0 :(得分:0)

以下汇总将返回您的整个文档,并按类型过滤注释:

  db.collection.aggregate([
  {
    $addFields: {
      comments: {
        $filter: {
          input: "$comments",
          as: "comment",
          cond: {
            $eq: [
              "$$comment.type",
              "ask"
            ]
          }
        }
      }
    }
  }
])