1

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

2

1 Answer 1

1

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]}
                    }
        }
        ])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.