Skip to content

Instantly share code, notes, and snippets.

@elvio
Created August 11, 2014 14:37
Show Gist options
  • Save elvio/8f93d837268ec52c04e7 to your computer and use it in GitHub Desktop.
Save elvio/8f93d837268ec52c04e7 to your computer and use it in GitHub Desktop.
Simple Mongo map-reduce
var mapFunction = function() {
emit(this.facebook_object_id, 1);
};
var reduceFunction = function(key, values) {
return Array.sum(values);
};
var options = {
out: {inline: 1},
};
function compare(v1, v2) {
if (v1.value < v2.value) {
return -1;
} else if (v1.value > v2.value) {
return 1;
} else {
return 0;
}
}
var results = db.interactions.mapReduce(mapFunction, reduceFunction, options).results;
var sorted = results.sort(compare).reverse().splice(0, 25);
@gobert
Copy link

gobert commented Aug 11, 2014

db ||= Mongo::Connection.new("localhost", 27017).db("taste_profiler_production")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment