Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def self.helpful_reporters(opts={})
- opts = {:limit => 5}.merge(opts)
- map = <<EOF
- function() {
- emit(this.username, this.votes);
- };
- EOF
- reduce = <<EOF
- function(key, values) {
- var total = 0;
- for(var i=0; i<values.length; i++) {
- total += values[i];
- }
- return total;
- };
- EOF
- collection.mapreduce(map, reduce, {:query => {:created_at => {"$gte" => 1.week.ago.utc}}, :out => "helpful_reporters"}).find.sort(["value", Mongo::DESCENDING]).limit(opts[:limit]).map do |reporter|
- {:username => reporter["_id"], :occurrences => reporter['value'].to_i}
- end
- end
Add Comment
Please, Sign In to add comment