Count distinct values in mongoDB


This is the document structure in mongoDB

{ "_id" :ObjectId("9elesdf3lk3jppefll34d210"), "category" :"data1",product:'data'} { "_id" :ObjectId("9elesdf3lk3jppefll34d211"), "category" : "data2",product:'data'} { "_id" :ObjectId("9elesdf3lk3jppefll34d211"), "category" : "data1",product:'data' }

where category is indexed. I want to take a distinct count of the category field.

Currently I am using the following code to take the counts

db.collection.aggregate( {$group : {_id : "$category"} }, {$group: {_id:1, count: {$sum : 1 }}})

This query was giving me proper counts but my database is increasing day by day and the query is taking longer to execute. Is there some other methodology to take the counts in a faster way?


As already pointed out by <a href="https://stackoverflow.com/users/1259510/johnnyhk" rel="nofollow">JohnnyHK</a>, use <a href="https://docs.mongodb.com/manual/reference/method/db.collection.distinct/#db-collection-distinct" rel="nofollow">db.collection.distinct</a> if possible as it provides the chance of <a href="https://docs.mongodb.com/manual/reference/method/db.collection.distinct/#index-use" rel="nofollow">leveraging indexes</a>

So in your case db.collection.distinct('category').length should be pretty fast.<br /> If you still suffer from performance issues then have a look at


to see the execution plan of the query and take actions on it or provide it to this question so that we see whether your index is actually used.


  • Density Value for each Return
  • Dimension issue with scipy's curve_fit function
  • How can I convert this tuple of tuples into a count of its elements?
  • What's included in the “Initialization time” in the Symfony2 web profiler?
  • Get highest value from a file using mSL and mIRC
  • How to override JAXB @XMLAccessorType(XMLAccessType.FIELD) specified at a Class level with @XMLEleme
  • Facebook Open Graph Story Custom Actions Keep Getting Rejected - Advice Please?
  • finding symmetric difference/unique elements in multiple arrays in javascript
  • Taking mean across rows grouped by a variable in numpy
  • Grouping by blank nodes
  • Query to get the Top 2 from each group
  • How to implement limit with Nhibernate and Sybase
  • Sending Content-Type application/x-www-form-urlencoded WSO2 ESB
  • R sqldf renaming a field in a select statement
  • Counting problem C#
  • Available space left on drive - WinAPI - Windows CE
  • Where these are stored?
  • abstracting over a collection
  • How can I tell a form not to dispose a particular control when it closes?
  • SAXReader not re-ecape characters
  • Using Sax parsing to edit and write XML in VB6
  • Redux Form - Not able to type anything in input
  • How can I set a binding to a Combox in a UserControl?
  • Django: Count of Group Elements
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Retrieving value from sql ExecuteScalar()
  • Javascript Callbacks with Object constructor
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • retrieve vertices with no linked edge in arangodb
  • Proper folder structure for lots of source files
  • KeystoneJS: Relationships in Admin UI not updating
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal