Created
July 11, 2010 23:27
-
-
Save wjessop/471938 to your computer and use it in GitHub Desktop.
This file contains 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
class VisitorSession | |
include Mongoid::Document | |
field :visits, :type => Integer, :default => 1 | |
field :project_id | |
def visits_for_project | |
map = <<EOF | |
function() { | |
if (this.project_id == '#{project_id}') { | |
emit(this.visits, {visits: this.visits}) | |
} | |
} | |
EOF | |
reduce = <<EOF | |
function(key, values) { | |
var sum = 0; | |
values.forEach(function(doc) { | |
sum += doc.visits; | |
}); | |
return {visits: sum}; | |
}; | |
EOF | |
collection.mapreduce(map, reduce).find().first["value"]["visits"].to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment