Skip to content

Instantly share code, notes, and snippets.

@bmaland
Created July 1, 2013 15:15
Show Gist options
  • Save bmaland/5901727 to your computer and use it in GitHub Desktop.
Save bmaland/5901727 to your computer and use it in GitHub Desktop.
var MongoClient = require('mongodb').MongoClient;
var mapFn = function() {
emit(1, { length: this.length });
}
var reduceFn = function(key, values) {
var sum = 0;
values.forEach(function(doc) {
sum += doc.length;
});
return { totalLength: sum };
};
MongoClient.connect('mongodb://10.1.4.175:27017/test', function(err, db) {
if(err) throw err;
var MR = {
mapreduce: "beards",
out: {inline: 1},
map: mapFn.toString(),
reduce: reduceFn.toString()
}
db.executeDbCommand(MR, function(err, dbres) {
console.log(dbres);
var results = dbres.documents[0].results
console.log("executing map reduce, results:")
console.log(JSON.stringify(results))
process.exit(1)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment