Database
[{"payment":{"transactions":[{"_id":"5d71d1ff2f7c8151c58c44cf","method":"paytm","amount":100,"paymentOn":"2019-09-06T03:26:44.959Z"},{"_id":"5d71d1ff2f7c8151c58c44ce","method":"cash","amount":650,"paymentOn":"2019-09-06T03:26:55.531Z"}],"status":"partial"},"customer":"5d66c434c24f2b1fb6772014","order":{"orderNumber":"WP-ORD-06092019-001","total":770,"balance":20}},{"payment":{"transactions":[{"_id":"5d71d1ff2f7c8151c58c44cf","method":"paytm","amount":100,"paymentOn":"2019-09-06T03:26:44.959Z"}],"status":"partial"},"customer":"5d66c434c24f2b1fb6772014","order":{"orderNumber":"WP-ORD-06092019-001","total":200,"balance":100}}]
Query
db.collection.aggregate([{"$unwind":"$payment.transactions"},{"$group":{"_id":"$_id","balance":{"$first":"$order.balance"},"paytm":{"$sum":{"$cond":[{"$eq":["$payment.transactions.method","paytm"]},"$payment.transactions.amount",0]}},"cash":{"$sum":{"$cond":[{"$eq":["$payment.transactions.method","cash"]},"$payment.transactions.amount",0]}}}},{"$group":{"_id":null,"balance":{"$sum":"$balance"},"cash":{"$sum":"$cash"},"paytm":{"$sum":"$paytm"}}}])