Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Filtering populated result

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"
            ]
        }
          
    ]
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!