How to count total number of records inside data using $group aggregation

Archana Sharma :

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?

Archana Sharma :

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the total number of records before I run limit when using Aggregation

How to construct Aggregation (Count) of records using NEST?

Count the total number of duplicate records

Laravel 5 - How to count total records with joins and group by

How to count total number of records after join the three tables in postgresql?

Perform an aggregation on JSON data in JavaScript. I have to get the total number of Records for each school and total Marks of School Records

How to count total number using if statement

How to get count of records using group by clause

Count number of records returned by group by

How to count total number of values as they appear inside a table

How to count total number of patients in a certain age group

how to get count of total number of rows following a group by?

How to group and count with MongoDB aggregation

How to count rating on group aggregation?

Count total number of records based on answers

Count total number of records in Rails join table

How to count number of records in a group and save them in a csv file?

How to count number of records in each group and add them to main dataset?

How to count the total number of reports for each month using stored timestamp data and output in a json array format

how to write a function in chaincode that simply count the total records and return total number.hyperledger fabric

Group into group and count using aggregation framework in MongoDB

How do i count number of referals using aggregation in mongoDB

How to get Laravel group array data total count?

Group By, Count and get Total by itens inside list

How to group by an array and determine total count of rows using javascript

How to group by month based on date using java and calculate total count?

How to find the total after using count and group_contat

How to convert MongoDB $group and count/$sum aggregation to a Java Object using Aggregation?

How to get total number of aggregation buckets in Elasticsearch?