How to query Total Count by Group By?

Dear All,
I am very new to MongoDB. I need your guy help.
I want total count group by intent with C# .NET.
Could you guys please help me the way how to do?
The below are the request and the response I want.

Thank you so much.

Request

[
   {
      "messages":[
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Appointment"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Thankyou"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Thankyou"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Thankyou"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Appointment"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Demo"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Demo"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Appointment"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Appointment"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Appointment"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Appointment"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Leads"
         }
      ]
   }
]

Response

{
"Appointment" : 6,
"Welcome" : 6,
"Thankyou" : 3
"Leads" : 1
"Demo" : 2
}

Best Regards,
Kyi Moh

Hi, @Kyimohmoh_Thwin,

Welcome to the MongoDB Community Forums. What you are trying to accomplish is a grouping operation. This can be expressed in MongoDB using an aggregation with a Group stage or using LINQ with GroupBy. Hopefully that helps get you started.

Sincerely,
James

Hi @James_Kovacs ,

Thanks you so much for your reply help.
Could you please provide the reference code with c#?

Best Regards,
Kyi Moh

Hi @James_Kovacs ,

I am using MongoDB C# Driver 2.15.
The Intent property I want to group by is located at the array list.
I was finding out it how to get. But, I find only simple group by field not in array list.
Could you please provide one the ways?

[
   {
      "messages":[
         {
            "intent":"Welcome"
         },
         {
            "intent":"Appointment"
         },
         {
            "intent":"Welcome"
         }
      ]
   },
   {
      "messages":[
         {
            "intent":"Thankyou"
         },
         {
            "intent":"Demo"
         }
      ]
   },
]

Best Regards,
Kyi Moh

Hi All,

Please help me the way? I need this urgent and I am very new to MongoDB. So I need your guy help.

Best Regards,
Kyi Moh

Dear @James_Kovacs
I got it with the below code

var pResults = collection.Aggregate()
                            .Unwind("messages")
                            .Match(new BsonDocument { { "version", "3.0" }})
                            .Group(new BsonDocument
                             {
                                   { "_id", "$messages.intent" },
                                   {"count", new BsonDocument("$sum", 1)}
                             })
                            .ToList();

Thank you

Best Regards,
Kyi Moh

Hi, @Kyimohmoh_Thwin,

We’re glad that you were able to find a solution based on the provided resources. While we cannot write code for you, we are always happy to point you in the right direction.

Sincerely,
James

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.