Me pregunto si puedo filtrar el array de visits , para no duplicar patient en las visitas (no tiene sentido si me sale un paciente y en sus visitas está su id). Quería filtrarlo pero dice patient.aggregate is not a function . ¿No puedo usar agregados aquí?
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) } })salida de corriente:
{ "__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" ] } ] }