Database
[{"user":1,"totalPrice":1,"isDelivered":true},{"user":1,"totalPrice":5,"isDelivered":false},{"user":1,"totalPrice":10,"isDelivered":true},{"user":1,"totalPrice":20,"isDelivered":true},]
Query
db.collection.aggregate([{$match:{}},{$group:{_id:"$user",TotalSpent:{$sum:"$totalPrice"},TotalOrders:{$sum:1},TotalCompletedOrderSpent:{$sum:{$cond:{if:{$eq:["$isDelivered",true]},then:"$totalPrice",else:0}}},TotalCompletedOrders:{$sum:{$cond:{if:{$eq:["$isDelivered",true]},then:1,else:0}}}}}])