Database
[{id:1,name:"Alfred",age:10,orders:[{type:"A",address:"Address1"},{type:"A",address:"Address2"},{type:"B",address:"Address4"},{type:"B",address:"Address5"},{type:"B",address:"Address6"}]},{id:2,name:"Bren",age:15,orders:[{type:"B",address:"Address1"},{type:"B",address:"Address2"},{type:"B",address:"Address4"},{type:"A",address:"Address5"},{type:"B",address:"Address6"}]}]
Query
db.collection.aggregate([{$unwind:"$orders"},{$group:{_id:{id:"$id",type:"$orders.type"},name:{$first:"$name"},age:{$first:"$age"},count:{$sum:1}}},{$group:{_id:"$_id.id",name:{$first:"$name"},age:{$first:"$age"},type:{$push:{k:{$concat:["countType","$_id.type"]},v:"$count"}}}},{"$replaceRoot":{"newRoot":{"$mergeObjects":["$$ROOT",{"$arrayToObject":"$type"}]}}},{$unset:"type"}])