I have the following Schema, I'm using the Aggregation Framework with mongoose in JS
{
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"},
...
]
}
I'm trying to get this output
{
id: 1,
name: Alfred,
age:10,
countTypeA:2,
countTypeB:3
},
{
id: 2
name: Bren,
age: 15,
countTypeA: 1,
countTypeB: 4
}
I've tried using $unwind and $group:{_id:"orders.type"} but I'm losing the original data (name, age) for each row, I don't mind losing the address information, I'm interested in counting the type of orders each user has inside their orders list.