dtl4521 2018-11-21 15:03
浏览 822
已采纳

如何使用$ group聚合计算数据内部记录的总数

I am trying to perform a query using golang mgo package to effectively get similar values from a $group aggregation and count the total record value.But I am not able to get total record count for grouped data. My structure is like this:

{

    "data": [
        {
            "_id": 366,

            "logs": [
                {
                    "id": 113,
                    "booking_id": 366,
                    "provider_id": 13,
                    "cid": 11,
                    "type": "basic",
                    "time": 1542793756,
                },
                {
                    "id": 116,
                    "booking_id": 366,
                    "provider_id": 13,
                    "cid": 0,
                    "type": "type2",
                }


            ]
        },
        {
            "_id": 362,

            "logs": [
                {
                    "id": 104,
                    "booking_id": 362,
                    "provider_id": 7,
                    "cid": 10,
                    "type": "basic",
                    "time": 1542776677,
                }
            ]
        },

        {
            "_id": 370,

            "logs": [
                {
                    "id": 111,
                    "booking_id": 370,
                    "provider_id": 9,
                    "cid": 11,
                    "type": "basic",
                    "time": 1542792661,
                },
                {
                    "id": 112,
                    "booking_id": 370,
                    "provider_id": 11,
                    "cid": 11,
                    "type": "basic",
                    "time": 1542793185,
                }
            ]
       }

    ],
     "total_record": 2
   }

For this I want total record preset inside data::

"total_record":3 

Query I am using ::

query := []bson.M{
        {"$group": bson.M{
        "_id":  "$booking_id",
        "logs": bson.M{ "$push": "$$ROOT" }
        "count": bson.M{"$sum":1},
        }},
    }

    pipe := getCollection.Pipe(query)
    err = pipe.AllowDiskUse().One(&result)

I want total count of this result. Log section having data with duplicate bookings with the different "provider_id" values but I have grouped all similar booking_id data in single document and show it's count. Is is possible with $group aggregation?

  • 写回答

1条回答 默认 最新

  • dongziya9863 2018-11-22 04:37
    关注

    You can use $count aggregation to find total record inside data, link

    To calculate total count of grouped data, you can use $count aggregation after grouping data. Your Query should be like this if you want total count data::

    query := []bson.M{
            {"$group": bson.M{
            "_id":  "$booking_id",
            }},
            {"$count" : "count"},
        }
    
        pipe := getCollection.Pipe(query)
        err = pipe.AllowDiskUse().One(&result)
    

    To calculate records inside each "logs" you can use query as follows::

    query := []bson.M{
            {"$group": bson.M{
            "_id":  "$booking_id",
            "logs": bson.M{ "$push": "$$ROOT" },
            "count": bson.M{"$sum":1},
            }},
        }
    
        pipe := getCollection.Pipe(query)
        err = pipe.AllowDiskUse().One(&result)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况