Created
August 11, 2014 14:37
-
-
Save elvio/8f93d837268ec52c04e7 to your computer and use it in GitHub Desktop.
Simple Mongo map-reduce
This file contains hidden or 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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
db ||= Mongo::Connection.new("localhost", 27017).db("taste_profiler_production")