title: Count distinct values in mongoDB
body: <p>This is the document structure in mongoDB</p>
  
  <pre><code>{ "_id" :ObjectId("9elesdf3lk3jppefll34d210"), "category" :"data1",product:'data'}
  { "_id" :ObjectId("9elesdf3lk3jppefll34d211"), "category" : "data2",product:'data'}
  { "_id" :ObjectId("9elesdf3lk3jppefll34d211"), "category" : "data1",product:'data' }
  </code></pre>
  
  <p>where <code>category</code> is indexed. I want to take a distinct count of the <code>category</code> field.</p>
  
  <p>Currently I am using the following code to take the counts</p>
  
  <pre><code>db.collection.aggregate( 
     {$group : {_id : "$category"} }, 
     {$group: {_id:1, count: {$sum : 1 }}})
  </code></pre>
  
  <p>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?</p>