I'm wondering if I'm able to filter the visits array, not to duplicate patient in the visits (it has no sense if I get a patient and in his/her visits there is his/her id). I wanted to filter it out but it says that patient.aggregate is not a function.
Am I not able to use aggregate here?
router.get("/:id", async (req,res) => {
try {
await Patient.findById(req.params.id)
.populate('visits')
.exec((err, patient) => {
if (err) {
return res.status(500).send("Error")
} else {
const patientFiltered = patient.aggregate([
{
$project: {
city: 1,
country: 1,
dateOfBirth: 1,
firstName: 1,
lastName: 1,
married: 1,
points: 1,
visits: {
$filter: {
cost: 1,
date: 1,
doctor: 1
}
}
}
}
])
return res.send(patientFiltered)
}
})
} catch (err) {
return res.send(err)
}
})
current output:
{
"__v": 0,
"_id": "619e777b1b7c2e173140c920",
"city": "Praga",
"country": "CZ",
"dateOfBirth": "1999-12-12T00:00:00.000Z",
"firstName": "Al",
"lastName": "Varo",
"married": true,
"points": 99,
"visits": [
{
"__v": 0,
"_id": "619ea657697f46c16cbde582",
"cost": 20,
"date": "2021-11-24T00:00:00.000Z",
"doctor": [
"619ea1139218eae32f3f8111"
],
"patient": [
"619e777b1b7c2e173140c920"
]
}
]
}