Created
June 7, 2010 09:52
-
-
Save kurokikaze/428485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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