У меня есть список вопросов для пациента, у каждого пациента есть идентификатор в mongoDB (PatientId). В каждом вопросе у меня много комментариев (я разделяю каждый комментарий по типу комментария: type = "ask" для пациента, отправьте комментарий-примечание, type = "reply" для доктора, отправьте ответный комментарий) Я хочу вернуть 2 массива комментариев но фильтруйте по type = "ask" & type = "reply". Мне нравится этот код ниже.
findQuestionByPatientId(req, res, next){
var limit = parseInt(req.query.limit);
if (isNaN(limit))
limit = 10;
var skip = parseInt(req.query.skip);
if (isNaN(skip))
skip = 0;
TextConsultation.aggregate([
{
$skip: skip
},
{
$limit: limit
},
{
$match: {
$and:[{"patientId":ObjectId(req.query.patientId)}]
}
},
{
$lookup:
{
from: "comments",
localField: "_id",
foreignField: "targetId",
as: "followups"
}
},
{
$lookup:
{
from: "comments",
localField: "_id",
foreignField: "targetId",
as: "answers"
}
},
{
$addFields: {
followups: {
$filter: {
input: "$followups",
as: "followup",
cond: { $eq: ['$$followup.type', "ask"]}
}
}
}
},
{
$addFields: {
answers: {
$filter: {
input: "$answers",
as: "answer",
cond: { $eq: ['$$answer.type', "reply"]}
}
}
}
},
{
$lookup:
{
from: "users",
localField: "askToDoctor",
foreignField: "_id",
as: "askToDoctorExtra"
}
},
{
$lookup:
{
from: "fileuploads",
localField: "attachment",
foreignField: "_id",
as: "attachmentExtra"
}
}
]).exec(function(err, obj){
if(err) {
res.send({ "data" : null,"resultCode": 0,"message":"Error : " + err });
return;
} else {
if(req.query.count == "1"){
TextConsultation.count(objFind, function(err, totalRecord){
res.send({"data":obj,"total":totalRecord,"message":""});
});
}else{
res.send({"data":obj,"message":""});
}
}
})
}
Ниже приведен результат.
{
"data": [
{
"_id": "5dce8a9d36ae900010449c28",
"patientId": "5c80930d447df7735138693e",
"title": "I got a flu, please give me an advice",
"askFor": "me",
"age": 27,
"gender": "",
"askContent": "My doctor, Please take care of me, I got a flu from yesteday.",
"attachment": "5dce8a9d36ae900010449c27",
"askToDoctor": "5cc169cc7294ecd858d1e735",
"createdBy": "5c80930d447df7735138693e",
"askStatus": "Awaiting Reply",
"approvedBy": "",
"approved": false,
"createdAt": "2019-11-15T11:23:09.210Z",
"updatedAt": "2019-11-15T11:23:09.210Z",
"__v": 0,
"followups": [
{
"_id": "5e0b2083caa0ed00107e1a4a",
"content": "sốt cao 38 độ",
"type": "ask",
"targetId": "5dce8a9d36ae900010449c28",
"approved": false,
"createdBy": "5c80930d447df7735138693e",
"followupFile": "5e9fef7e38381c00108d3ac2", // I want to populate this
"approvedBy": "5c80930d447df7735138693e",
"createdAt": "2019-12-31T10:18:43.938Z",
"updatedAt": "2020-04-22T07:17:18.360Z",
"__v": 0
},
{
"_id": "5e9fe85938381c00108d3ab8",
"content": "Test upfile",
"type": "ask",
"targetId": "5dce8a9d36ae900010449c28",
"createdBy": "5c80930d447df7735138693e",
"followupFile": "5e9fe85938381c00108d3ab7",// I want to populate this
"createdAt": "2020-04-22T06:46:49.575Z",
"updatedAt": "2020-04-22T06:46:49.575Z",
"__v": 0
},
{
"_id": "5ea001e238381c00108d3ac8",
"content": "xxxxxx",
"type": "ask",
"targetId": "5dce8a9d36ae900010449c28",
"createdBy": "5c80930d447df7735138693e",
"followupFile": "5ea001e238381c00108d3ac7",// I want to populate this
"createdAt": "2020-04-22T08:35:46.727Z",
"updatedAt": "2020-04-22T08:35:46.727Z",
"__v": 0
}
],
"answers": [
{
"_id": "5e0b1e81caa0ed00107e1a48",
"content": "OK, the weather is not good, please take care and take medicine that I give for you.",
"type": "reply",
"targetId": "5dce8a9d36ae900010449c28",
"approved": false,
"createdBy": "5d1cd947231ceb95b8838c1b",
"followupFile": null,
"approvedBy": "5d1cd947231ceb95b8838c1b",
"createdAt": "2019-12-31T10:10:09.196Z",
"updatedAt": "2019-12-31T10:10:09.196Z",
"__v": 0
}
],
"askToDoctorExtra": [],
"attachmentExtra": [
{
"_id": "5dce8a9d36ae900010449c27",
"filename": "attachment-1573816989205.png",
"originalName": "health_profile_01.png",
"description": "Doctor Consultation - Single upload",
"createdAt": "2019-11-15T11:23:09.210Z",
"updatedAt": "2019-11-15T11:23:09.210Z",
"__v": 0
}
]
}
],
"total": 7,
"message": ""
}
Я хочу, чтобы поле "followupnoteFile" было как показано ниже:
"followupFile": {
"_id": "5db1b42720b4090010738421",
"filename": "followupFile-1571927079085.png",
"originalName": "note.png",
"description": "follow-up note single upload",
"createdAt": "2019-10-24T14:24:39.088Z",
"updatedAt": "2019-10-24T14:24:39.088Z",
"__v": 0
},
Проблема в том, что «followupFile» - это объект файла, в него входят поля «_id», «filename», «originalName» и «description», поэтому я хотел бы заполнить «folowupFile», как мне это сделать? Большое спасибо!