How python lists are filtered using a similar method of mongodb aggregation $group

[
     {"_id": ObjectId("5abb4f9ca7e2c54c757b3e48"),
      "amount": 8400,
      "buyerEmail": "otzYzwMh24edWk8NxSJOqCSZREe0",
      "from": "weixin",
      "orderDate": datetime.datetime(2018, 3, 28, 8, 17, 28, 940000),
      "orderid": "2018032816173212079",
      "real": 8400,
      "rechargeDate": datetime.datetime(2018, 3, 28, 8, 17, 41, 877000),
      "status": 1,
      "tradeNo": "4200000099201803287230332578",
      "uid": ObjectId("5abb36051a62067bf7e30178")
    },
    {"_id": ObjectId("5b6699f6df03ec3294d7c0a4"),
      "amount": 100,
      "buyerEmail": "otzYzwC3YwRdu7QrWLXqS3VRJybI",
      "from": "weixin",
      "orderDate": datetime.datetime(2018, 8, 5, 6, 32, 21, 442000),
      "orderid": "2018080514322245193",
      "real": 100,
      "rechargeDate": datetime.datetime(2018, 8, 5, 6, 33, 0, 381000),
      "status": 1,
      "tradeNo": "4200000148201808052403940202",
      "uid": ObjectId("5a5738411a62061972e128cb")
    },
    .....
]

how to integrate the same uid and display only the elements of uid real from rechargeDate?
$group similar to mongodb aggregation
ideal result:

[
    {"uid":xxxxxxxxxxxxxxxx:
        [
            {
                "real":xxx,
                "from":xxx,
                "recharge":xxx,
            },
            {
                "real":xxx,
                "from":xxx,
                "recharge":xxx,
            },
            {
                "real":xxx,
                "from":xxx,
                "recharge":xxx,
            },
        ]
    },
        {"uid":xxxxxxxxxxxxxxxx:
        [
            {
                "real":xxx,
                "from":xxx,
                "recharge":xxx,
            },
            {
                "real":xxx,
                "from":xxx,
                "recharge":xxx,
            },
            {
                "real":xxx,
                "from":xxx,
                "recharge":xxx,
            },
        ]
    },
    ...
]
Apr.17,2021

use collections of python

import json
from collections import defaultdict


def main():
    ret = defaultdict(list)
    data = [
     {'_id': '5abb4f9ca7e2c54c757b3e48',
      'amount': 8400,
      'buyerEmail': 'otzYzwMh24edWk8NxSJOqCSZREe0',
      'from': 'weixin',
      'orderid': '2018032816173212079',
      'real': 8400,
      'status': 1,
      'tradeNo': '4200000099201803287230332578',
      'uid': '5abb36051a62067bf7e30178' -sharp ObjectId
    },
    {'_id': '5b6699f6df03ec3294d7c0a4',
      'amount': 100,
      'buyerEmail': 'otzYzwC3YwRdu7QrWLXqS3VRJybI',
      'from': 'weixin',
      'orderid': '2018080514322245193',
      'real': 100,
      'status': 1,
      'tradeNo': '4200000148201808052403940202',
      'uid': '5a5738411a62061972e128cb' -sharp ObjectId
    },
    {'_id': '5b6699f6df03ec3294d7c0a4',
      'amount': 200,
      'buyerEmail': 'otzYzwC3YwRdu7QrWLXqS3VRJybI',
      'from': 'weixin',
      'orderid': '2018080514322245193',
      'real': 200,
      'status': 1,
      'tradeNo': '4200000148201808052403940202',
      'uid': '5a5738411a62061972e128cb' -sharp ObjectId
    },
    ]
    
    for d in data:
        ret[d.get('uid')].append(d)
        
    print(json.dumps(ret, indent=2))
if __name__ == '__main__':
    main()
    
Menu