Mongodb iterative methods inside aggregate pipeline

  Kiến thức lập trình

Hi so I am working on a database and we have 100k entries where the structure looks similar to

dict = {
        'testResults': {
                     'test_1':1, #1 or 0 if a test passed/failed respectively
                     'test_2':0,
                     'test_3':1,
                      ....,
                     'test_N':1,
                    },
}

so far i have an aggregate pipline like:

result = list(mycol.aggregate([
        {
            "$group": {
                '_id': f'{testname}',
                'pass': {"$sum":f"$individual_test_outcomes.{testname}"},
                'count': {"$sum":1},
            },
            
        },
        {
          "$addFields":{
                'passRate': {"$divide":["$pass","$count"]},
            },  
        }
    ]))

and this gives me the number of how many files passed a particular test, the total number of files, and the pass rate.

This gives me an output like

{
 '_id': 'test_1',
 'pass': 50,
 'count':100,
 'rate':0.5
}

but there are many tests besides test one, so I want an output that is like:

{
{
 '_id': 'test_1',
 'pass': 50,
 'count':100,
 'rate':0.5
}, 
{
 '_id': 'test_2',
 'pass': 50,
 'count':100,
 'rate':0.5
},
...
{
 '_id': 'test_N',
 'pass': 50,
 'count':100,
 'rate':0.5
}
}

my solution was to write a function and pass all the test names into a for loop and appending the results, but I am worried that this will be too slow and inefficient.

I was wondering if anyone knew how to write an iterative method in the aggregate to achieve the desired results

New contributor

SC990987 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT