{"version":"1.0","type":"rich","title":"MongoDB equivalent of an SQL query to get the distinct values of a field in a collection including the count of documents which have each distinct value (distinct with count)","description":"This example demonstrates how to retrieve distinct values of a field in a MongoDB collection along with the document count for each value, mirroring a SQL `GROUP BY` query. It shows two aggregation pipelines using MongoDB’s `aggregate()` method with the `$group` stage to group by the field `_id` and compute a `count` via `$sum:1`, and an optional `$sort` stage to order the results by count descending. The first pipeline returns unsorted counts, while the second sorts them from highest to lowest. No external visualization library is used; the output is raw aggregated data from the database.","provider_name":"VizHub AI Studio","provider_url":"https://next.vizhub.com","html":"<iframe src=\"https://next.vizhub.com/viz/gist-fb7531c7cf1a4da833aaba69d82718d0?mode=embed\" width=\"960\" height=\"500\" scrolling=\"no\" frameborder=\"1\"></iframe>","width":960,"height":500,"cache_age":3600}