I have json object in my db like as follows:
{
'name':'test1',
'game':'cric'
},
{
'name':'test1',
'game':'cric'
},
{
'name':'test1',
'game':'football'
},
{
'name':'test2'
'game':'football'
}
I am trying to get output as follows
{
'name':'test1'
'game':[{cric: 2}, {football:1}],
'totalCount': 3
}
I used aggregate query for that.
group {'_id':{name:'$name'}, {game:{$addToSet:$game}}
project {name : $name, game: $game}
I got output as
{name: 'test1', 'game':[cric, football]}
Now i have question that, how can i get count of game. it current example for cricket it is 2 and for football 1 for test1 user