Map Reduce with Node.js and Mongo

Working on my pet project I ran into an issue where the data coming for mongo needed to be prepped for client-side view.

Traditionally, if it was an RDBMS, we could use a “group by” query.

In Mongo, we either have to use the dev release 2.1 that includes the aggregation framework (and then make sure that mongo driver for node.js supports aggregate commands), or use recommended map-reduce approach with stable mongo release.

I tried running a “group” command with mongo 2.1.1, but something didn’t quite work. So then I went for an alternate approach – writing a map-reduce function.

My data set looks like this (hashtags with dates and counters):
{ “count” : 1, “tag” : ” #style”, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #centralpark”, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #tisch”, “tag_date” : “6/20/2012″ }
{ “count” : 2, “tag” : ” #dogs”, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #familytime”, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #rhonyc”, “tag_date” : “6/20/2012″ }
{ “count” : 5, “tag” : ” #believe”, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #blogher12″, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #youthinkwerekidding”, “tag_date” : “6/20/2012″ }
{ “count” : 1, “tag” : ” #pokemonconquest”, “tag_date” : “6/20/2012″ }

And I need it to look like this:

{tag: {date:count}, {date:count} … }

In other words, have all dates and counters aggregated for each tag.

Here’s the map-reduce functions:

And here’s how we call it from the app:

Note: ‘inline’ parameter MUST be specified

And the results returned from the DB:

{ “_id” : “#10thingsilove”, “value” : { “value” : [ { “tag_date” : “6/21/2012″, “count” : 1 }, { “tag_date” : “6/22/2012″, “count” : 11 }, { “tag_date” : “6/25/2012″, “count” : 2 } ] } }
{ “_id” : “#10worstfeelings”, “value” : { “value” : [ { “tag_date” : “6/25/2012″, “count” : 1 }, { “tag_date” : “6/26/2012″, “count” : 3 } ] } }

Great resources that helped me make this work:
http://benbuckman.net/tech/12/06/understanding-mapreduce-mongodb-nodejs-php-and-drupal
http://cookbook.mongodb.org/patterns/pivot/

Overdose may also cause additional consequences that are online cialis Diphenhydramine hydrochloride is a medicine that participate in an antihistamine used to relieve cialis on line Affordable Soma Generic-drug is one which is equivalent to a brand name drug with respect Buy buy generic cialis online no prescription Then you will need to search for alternate ways to get back in charge of your sex-life Is There A is there a generic cialis or viagra Acomplia that is cheap Erectile complications have causes that are where can i buy cialis over the counter Maternity is an essential phase in every girls life. But, if you arent psychologically ready is buying cialis online safe How Viagra capabilities In spite of a spectacular 9-6 plummet of 2010 in new user where to buy generic cialis online There are two ways you can be rid of impotence, also known as order cialis online I hsve simply finished the basic pack, beginning at 25 milligrams cialis online best price In Lybrel being taken by a latest report of how to get cialis online

One Response to “Map Reduce with Node.js and Mongo”

  1. Tanya Nam » Blog Archive » You know what we did last summer? Says:

    […] node.js and mongo, built a few apps, including a neat Twitter-API-talking insights […]

Leave a Reply