Skip to content

Instantly share code, notes, and snippets.

@kurokikaze
Created June 4, 2010 12:48
Show Gist options
  • Save kurokikaze/425369 to your computer and use it in GitHub Desktop.
Save kurokikaze/425369 to your computer and use it in GitHub Desktop.
db.open(function(err, db) {
db.collection('rounds', function(err, collection) {
var name_letter = 'v';
var map = function() {
if (this.score1 > this.score2) {
emit(this.player1, this.score1);
} else {
if (this.score2 > this.score1) emit(this.player2, this.score2)
}
};
var reduce = function (k, vals) {var sum = 0;for(var i in vals) sum += vals[i];return (sum / vals.length); };
collection.mapReduce(map, reduce, function(err, result) {
if (err) {
sys.puts('Err: ' + JSON.stringify(err));
} else {
result.findOne({'_id':'kze'}, function(err, scores) {
if (err) {
sys.puts('Err in results');
}
sys.puts('Kaze scores: ' + JSON.stringify(scores));
});
sys.puts('Mapreduce done');
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment