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

SELECT COUNT(DISTINCT(userID)) FROM SavedVehicle;

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.

db.SavedVehicle.count();
db.SavedVehicle.distinct("userID");

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

db.SavedVehicle.distinct('userID').length

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

To further enhance by adding a where clause:

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

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

About these ads

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 129 other followers

%d bloggers like this: