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












1















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?










share|improve this question

























  • It's not clear to me what you want to count.

    – icza
    Nov 21 '18 at 17:53











  • I want total_record present inside data

    – Archana
    Nov 22 '18 at 3:43











  • If I am using "count": bson.M{"$sum":1} it will return records inside logs, but I want to get total record inside data, which is 3

    – Archana
    Nov 22 '18 at 3:48
















1















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?










share|improve this question

























  • It's not clear to me what you want to count.

    – icza
    Nov 21 '18 at 17:53











  • I want total_record present inside data

    – Archana
    Nov 22 '18 at 3:43











  • If I am using "count": bson.M{"$sum":1} it will return records inside logs, but I want to get total record inside data, which is 3

    – Archana
    Nov 22 '18 at 3:48














1












1








1








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?










share|improve this question
















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?







mongodb go mgo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 4:08







Archana

















asked Nov 21 '18 at 15:03









ArchanaArchana

352214




352214













  • It's not clear to me what you want to count.

    – icza
    Nov 21 '18 at 17:53











  • I want total_record present inside data

    – Archana
    Nov 22 '18 at 3:43











  • If I am using "count": bson.M{"$sum":1} it will return records inside logs, but I want to get total record inside data, which is 3

    – Archana
    Nov 22 '18 at 3:48



















  • It's not clear to me what you want to count.

    – icza
    Nov 21 '18 at 17:53











  • I want total_record present inside data

    – Archana
    Nov 22 '18 at 3:43











  • If I am using "count": bson.M{"$sum":1} it will return records inside logs, but I want to get total record inside data, which is 3

    – Archana
    Nov 22 '18 at 3:48

















It's not clear to me what you want to count.

– icza
Nov 21 '18 at 17:53





It's not clear to me what you want to count.

– icza
Nov 21 '18 at 17:53













I want total_record present inside data

– Archana
Nov 22 '18 at 3:43





I want total_record present inside data

– Archana
Nov 22 '18 at 3:43













If I am using "count": bson.M{"$sum":1} it will return records inside logs, but I want to get total record inside data, which is 3

– Archana
Nov 22 '18 at 3:48





If I am using "count": bson.M{"$sum":1} it will return records inside logs, but I want to get total record inside data, which is 3

– Archana
Nov 22 '18 at 3:48












1 Answer
1






active

oldest

votes


















0














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)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414900%2fhow-to-count-total-number-of-records-inside-data-using-group-aggregation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 4:37









        ArchanaArchana

        352214




        352214
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414900%2fhow-to-count-total-number-of-records-inside-data-using-group-aggregation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory