Skip to content

Instantly share code, notes, and snippets.

@wjessop
Created July 11, 2010 23:27
Show Gist options
  • Save wjessop/471938 to your computer and use it in GitHub Desktop.
Save wjessop/471938 to your computer and use it in GitHub Desktop.
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