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

142
Views
How to loop over a field's value one after another and do aggregation operations on it?

I recently started working with MongoDB and its aggregation pipeline. I need some help with looping over the field called Id and doing aggregation operation on it one after another directly inside pipeline.

Currently I am doing something like this (in Node.js):

let patients = await client.db('MyDB').collection('Patients_MA').find().toArray();
let patientColl = client.db('MyDB').collection('Patients_MA');

for (int i=0; i<patients.length; i++)
{
    doc = patientColl.aggregate([
      {
        $match: {Id: patients[i].Id}
      }, 
      {
        $project: {
          _id: 0, 
          num: {
            $add: [
              {$multiply: [0.7, 2]},
              {$multiply: [0.2, 2]},
              {$multiply: [0.15, 0]}
            ]
          }
        }
      }
    ]);

    await patientColl.updateOne(
        { "Id": id },
        { "$set": {'heuristic': num} }
    );
}

For the sake of easy understanding I have condensed the pipeline to just the main thing I want it to do which is to update the heuristic value but in my original pipeline, each Id will have a different heuristic value. Also, I have approximately 8000 patients Ids and in my original pipeline, I am working with 3 different collection.

So my question is, is there a way to loop over each Id's, run the the pipeline above, and update the heuristic value directly inside the aggregation pipeline? Or a way to do this more efficiently without having to make O(8000) calls?

Edit: Here's the link to the original pipeline.

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!