Kazen
Kazen

Reputation: 21

Merging multiple aggregation queries to one with MongoDB

I'm using these three queries to can have a python dataframe format with the columns : 'Date', '% part of business 2', '% part of business 3'. (for each day to have the percentage of gain from business 2 and 3).

query_business2 = collection.aggregate( [
        {
          '$match': {'Business': 2}      
        },
        {
                '$group': {
                        '_id': '$Date',
                        'stab2': {'$sum': '$Money'}
                }
        },
        {
                '$sort': {'_id': 1}
        }
        ])
query_business3 = collection.aggregate([
        {
                '$match': {'Business':3}
        },
        {
                '$group': {
                        '_id': '$Date',
                        'stab3': {'$sum': '$Money'}
                        }
        },
        {
                '$sort': {'_id': 1}
        }
        ])
query_total = collection.aggregate([
        {
                '$group': {
                        '_id': '$Date',
                        'total': {'$sum': '$Money'}
                        }
        },
        {
                '$sort': {'_id': 1}
        }
        ])

For this to be faster, I would like to merge these three queries into one. I tried using '$or' but didn't work for unashable dict. Is there a better way to do that ? It might be possible to directly make the dataframe format without using pandas after this queries and to calculate directly the percentage of each business compared to the total money earned. Thank you for your help

Upvotes: 1

Views: 814

Answers (1)

Kazen
Kazen

Reputation: 21

Thanks to prasad_ the answer is :

query_business = collection.aggregate([
        {
            '$group':{
                    '_id': '$Date',
                    'total_2': {'$sum' : {'$cond': [{'$eq': ['$Business', 2]}, '$Money', 0]}},
                    'total_3': {'$sum' : {'$cond': [{'$eq': ['$Business', 3]}, '$Money', 0]}},
                    'total': {'$sum': '$Money'},
                    }
        },
        {
            '$match': {'$and': [{ 'total_2': {'$gt': 0}}, {'total': {'$gt': 0}},{'total_3':{'$gt':0}}]}     
        },
        {
            '$addFields':{
                    'part_2': { "$multiply": [ { "$divide": ["$total_2","$total"] }, 100 ] },
                    'part_3': { "$multiply": [{'$divide': ['$total_3','$total']}, 100]}
                    }
        }
        ])

Upvotes: 1

Related Questions