Database

db={"users":[{"_id":"user1","fullName":"test 1","phone":"8494948432","phoneCode":"+91",},{"_id":"user2","fullName":"test 2","phone":"8494948435","phoneCode":"+81",},{"_id":"user3","fullName":"test 3","phone":"8494948435","phoneCode":"+81",}],"orders":[{"orderId":"ODR0000000487","status":"delivered","user":"user1"},{"orderId":"ODR0000000488","status":"delivered","user":"user1"},{"orderId":"ODR0000000487","status":"undelivered","user":"user1"},{"orderId":"ODR0000000487","status":"undelivered","user":"user2"},{"orderId":"ODR0000000488","status":"delivered","user":"user2"},{"orderId":"ODR0000000488","status":"delivered","user":"user3"}]}

Query

db.users.aggregate([{$lookup:{from:"orders",localField:"_id",foreignField:"user",as:"orders"}},{$unwind:"$orders"},{$group:{_id:{"userId":"$_id"},"data":{"$first":"$$ROOT"},"delivered":{"$sum":{"$cond":[{"$eq":["$orders.status","delivered"]},1,0]}},"undelivered":{"$sum":{"$cond":[{"$eq":["$orders.status","undelivered"]},1,0]}}}},{$project:{"_id":"$_id.userId","fullName":"$data.fullName","phone":"$data.phone","phoneCode":"$data.phoneCode","delivered":"$delivered","undelivered":"$undelivered"}}])

Result