如何使用聚合组和猫鼬计数?

时间:2022-04-27 20:47:33

I have json object in my db like as follows:

我的数据库中有json对象,如下所示:

{
'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

现在我有疑问,我怎么能得到比赛的数量。它是板球的当前示例,它是2,对于test1用户的足球1

2 个解决方案

#1


1  

A similar question is answered here.

这里回答了类似的问题。

For your particular case it would be:

对于您的特定情况,它将是:

db.collection.aggregate([
  {
    $group: {
      _id:   { name: "$name", game: "$game" },
               games: { "$push": "$game" },
      total: { "$sum": 1 }
    }
  },
  {
    $group: {
      _id: { name: "$_id.name" },
      games: { $addToSet: { game: "$_id.game", sum:"$total" } } 
    }
  }
 ])

And the result should look like:

结果应如下所示:

{
    "result" : [ 
        {
            "_id" : {
                "name" : "test1"
            },
            "games" : [ 
                {
                    "game" : "cric",
                    "sum" : 2
                }, 
                {
                    "game" : "football",
                    "sum" : 1
                }
            ]
        }, 
        {
            "_id" : {
                "name" : "test2"
            },
            "games" : [ 
                {
                    "game" : "football",
                    "sum" : 1
                }
            ]
        }
    ],
    "ok" : 1
}

#2


1  

Here is my solution to test1 name record

这是我对test1名称记录的解决方案

    db.getCollection('COLLECTION_NAME').aggregate([
      {$match : {name : "test1"}},
      {
        $group: {
         _id:  "$game" ,
         total: { "$sum": 1 },
         name :  {$first : "$name"}
        }
      },
      {
       $group: {
        _id : "$name",
        games: { $addToSet: { game: "$_id", sum:"$total" } },
        totalCount : {$sum : "$total"}
      }
     }
   ])

#1


1  

A similar question is answered here.

这里回答了类似的问题。

For your particular case it would be:

对于您的特定情况,它将是:

db.collection.aggregate([
  {
    $group: {
      _id:   { name: "$name", game: "$game" },
               games: { "$push": "$game" },
      total: { "$sum": 1 }
    }
  },
  {
    $group: {
      _id: { name: "$_id.name" },
      games: { $addToSet: { game: "$_id.game", sum:"$total" } } 
    }
  }
 ])

And the result should look like:

结果应如下所示:

{
    "result" : [ 
        {
            "_id" : {
                "name" : "test1"
            },
            "games" : [ 
                {
                    "game" : "cric",
                    "sum" : 2
                }, 
                {
                    "game" : "football",
                    "sum" : 1
                }
            ]
        }, 
        {
            "_id" : {
                "name" : "test2"
            },
            "games" : [ 
                {
                    "game" : "football",
                    "sum" : 1
                }
            ]
        }
    ],
    "ok" : 1
}

#2


1  

Here is my solution to test1 name record

这是我对test1名称记录的解决方案

    db.getCollection('COLLECTION_NAME').aggregate([
      {$match : {name : "test1"}},
      {
        $group: {
         _id:  "$game" ,
         total: { "$sum": 1 },
         name :  {$first : "$name"}
        }
      },
      {
       $group: {
        _id : "$name",
        games: { $addToSet: { game: "$_id", sum:"$total" } },
        totalCount : {$sum : "$total"}
      }
     }
   ])