Guest User

Untitled

a guest
Jan 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. def self.helpful_reporters(opts={})
  2. opts = {:limit => 5}.merge(opts)
  3. map = <<EOF
  4. function() {
  5. emit(this.username, this.votes);
  6. };
  7. EOF
  8. reduce = <<EOF
  9. function(key, values) {
  10. var total = 0;
  11. for(var i=0; i<values.length; i++) {
  12. total += values[i];
  13. }
  14. return total;
  15. };
  16. EOF
  17. 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|
  18. {:username => reporter["_id"], :occurrences => reporter['value'].to_i}
  19. end
  20. end
Add Comment
Please, Sign In to add comment