Select Distinct Count in MongoDB

A fairly standard query for a RDBMS user is writing a DISTINCT with COUNT() to get the number of unique rows that match.

For a scenario where users save vehicles into their account, we record


would simply get the number of unique userIDs in the SavedVehicle table, thus providing stats on how many unique users have saved a vehicle, removing the skew you would get when users save more than one vehicle.

So, how would we do this in Mongo?

MongoDB provides the option to do both count() and Distinct()…but not at the same time. See here (aggregation-mapreduce-in-mongo) for how to do these commands individually in Mongo.


Easiest way to use both at the same time is to enhance the query with some basic javascript. If the query is simple enough then the javascript method should suffice, if not then use Map-Reduce. More details here


The above will return all unique userID’s in the SavedVehicle collection.

To further enhance by adding a where clause:

db.SavedVehicle.distinct('userID', {'' : 'cars'}).length

which will return all unique users for saved vehicles which have a channel criteria of car


2 Responses to Select Distinct Count in MongoDB

  1. Rogério Carrasqueira says:

    Hi! I have an email database using MongoDB and I would like to count by email domain. I want to know how many is from Yahoo, Gmail etc. Is there exists some way to get this information using mongodb?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: