Question about grouping

I am referring to the * [Lecture: Aggregation Framework] when the lecturer managed to get countries that exist in the listing.
However, I am wondering if the database is bigger, there will be more countries in the listings. Is there a way for me to count the number of distinct countries using mongo shell? From what I understand, I can only get the number of listing for each country but I am not sure how to count the no. of distinct countries in the listings.

Thank you.

share the sample document and the expected output.

You do a $group stage with _id:"$country" using the $first accumulator. The output of this will be a list of documents, one per country, that is a list of distinct countries. You can then use a $count stage to determine the count.

You don’t need to use $first - just doing {$group:{_id:"$country"}} will give you one doument per country which you can then $count

Asya

3 Likes

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