Benjamin Lowenstein

Entrepreneur. Geek. Teacher. Founder, Colingo

MapReduce with MongoDB & Node.js

Here’s some generic code for doing map-reduce queries in node using the node-mongodb-native driver.

Suggestions welcome!

Good resoureces:
http://www.mongodb.org/display/DOCS/MapReduce
http://kylebanker.com/blog/2009/12/mongodb-map-reduce-basics/
https://github.com/christkv/node-mongodb-native

var mongodb = require('mongodb'),
    server = new mongodb.Server("localhost", 27017, {}),
    sys = require('util'),
    db = new mongodb.Db('MY_DATABASE_NAME', server, {}),

db.open(function (error, client) {
  if (error) throw error;
  var mapFn = function(){ 
      var n = this.someArray.length
      emit(
          this._id,
          { 
              someArrayVals : (n > 0 ? n : 0)
          } 
      );
  }
  var reduceFn = function(key, values) {
    var sum = 0;
    var rows = 0;
    values.forEach(function(doc) {
      sum += doc.someArrayVals;
      rows += 1;
    });
    return {someArrayVals: sum, rows : rows  };
  };

  var MR = {
      mapreduce: "my_collection", 
      out:  { inline : 1 },
      map: mapFn.toString(),
      reduce: reduceFn.toString()
  }

  db.executeDbCommand(MR, function(err, dbres) {
      var results = dbres.documents[0].results
      console.log("executing map reduce, results:")
      console.log(JSON.stringify(results))
      process.exit(1)
  })
})