Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var mongo = require('../lib/mongodb'),
sys = require('sys');
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : mongo.Connection.DEF
AULT_PORT;
sys.puts("Connecting to " + host + ":" + port);
var db = new mongo.Db('test', new mongo.Server(host, port, {}), {});
db.open(function(err, db) {
db.collection('transactions', function(err, collection) {
var name_letter = 'v';
var map = function() { emit('sum', this.amount) }
// var reduce = function (k, vals) {var sum = 0;for(var i in vals) sum += vals[i];return (sum / vals.length); };
var reduce = function(k, vals) { var sum = 0; for (var i in vals) { sum += vals[i] } return sum; }
collection.mapReduce(map, reduce, function(err, result) {
if (err) {
sys.puts(JSON.stringify(err));
} else {
result.findOne({'_id':'sum'}, function(err, scores) {
if (err) {
sys.puts('Err in results');
}
sys.puts('Scores: ' + JSON.stringify(scores));
process.exit();
});
sys.puts('Mapreduce done');
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.