【发布时间】:2020-02-10 09:47:40
【问题描述】:
我有一个患者的问题列表,每个患者在 mongoDB (patientId) 中都有一个 ID。在每个问题中,我都有很多 cmets(我将每个评论按评论类型分开:type = "ask" 用于患者后续注释评论,type = "answer" 用于医生评论) 我想返回一个评论数组,但按 type =“ask”过滤,我该怎么做。目前,我只返回所有 cmets 的数组,包括患者和医生。我正在使用这样的聚合和查找。 如何按类型过滤?请帮我?谢谢
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 ": ""
}
【问题讨论】:
-
请提供样品
-
你能在mongoplayground.net提供一个示例游乐场吗?
-
我已经修改了我的问题,包括提供上面的示例。请检查我的问题
标签: node.js mongodb express mongoose mongodb-query