A couple of weeks ago I promised a fun application in mongodb. That time has arrived. Suppose you have a collection of records and you want to group by some id (canonicalized url, user id, checkin venue) and see how many user actions (perhaps clicks, status updates, or checkins for the three examples listed) are associated with each. Those familiar with mongodb would ask, “Why not do this with the group() function?” Its limited to 20,000 unique ids. Many applications have more than this, so map/reduce is the way to go. Below I provide code to do this map reduce and then sort by value. From the shell sort is done as:
but if you run this from within python using the pymongo driver you will receive the error:
TypeError: if no direction is specified, key_or_list must be an instance of list
If this occurs make sure you sort with:
Some more hardcore/sophisticated mongodb examples/applications will be coming soon!