One query SQL users are pretty used to writing is DISTINCT with COUNT() to get the number if distinct (unique) rows that match a statement like …
SELECT COUNT(DISTINCT(PageURL)) FROM LogTable;
This rather simple query will get us back the number of unique PageURL’s in our LogTable … not an amazingly useful query but you get the point.
Getting Back Distinct Counts
Since MongoDB is often used for logging you might find you will often need such a query to get quick stats your boss just asked you for (but he “needed like yesterday”) … so how do we do this in MongoDB?
This is where things get a little weird if you are used to SQL. Basically (as of the writing of this blog post) there is no actual way to do the same thing in MongoDB.
However that doesn’t mean it isn’t fairly to get your count … you just need to start thinking a little differantly!
MongoDB provides both count() and distinct() …
Fire up your shell and type in:
This will query the number of distinct pageUrl’s in your Collection and then get the “length” of the cursor (of documents) that is returned … providing you with the distinct count.
Note: don’t add the () to length or you will throw an error.