【发布时间】:2017-02-27 21:17:18
【问题描述】:
我的数据库中有 json 对象,如下所示:
{
'name':'test1',
'game':'cric'
},
{
'name':'test1',
'game':'cric'
},
{
'name':'test1',
'game':'football'
},
{
'name':'test2'
'game':'football'
}
我试图得到如下输出
{
'name':'test1'
'game':[{cric: 2}, {football:1}],
'totalCount': 3
}
我为此使用了聚合查询。
group {'_id':{name:'$name'}, {game:{$addToSet:$game}}
project {name : $name, game: $game}
我的输出为
{name: 'test1', 'game':[cric, football]}
现在我有一个问题,我怎样才能获得游戏计数。它当前的板球示例是 2,对于 test1 用户来说足球 1
【问题讨论】: