Skip to content

Instantly share code, notes, and snippets.

@liushooter
Forked from jrdi/patient.rb
Last active August 29, 2015 14:18
Show Gist options
  • Save liushooter/8bb5fba2208a873b58b3 to your computer and use it in GitHub Desktop.
Save liushooter/8bb5fba2208a873b58b3 to your computer and use it in GitHub Desktop.
class Patient
include Mongoid::Document
field :created_at, type: Time
field :updated_at, type: Time
def self.group_by(field, format = 'day')
reduce = %Q{
function(key, values) {
var result = { count: 0 };
values.forEach(function(value) {
result.count += value.count;
});
return result;
}
}
Patient.map_reduce(Patient.map(field, format), reduce).out(inline: true)
end
def self.map(date_field, format = 'day')
date_format = case format
when 'month'
"mm + '/' + yyyy"
when 'year'
"yyyy"
else
"dd + '/' + mm + '/' + yyyy"
end
%Q{
function() {
var dateTime = new Date(this.#{date_field});
var dd = dateTime.getDate();
var mm = dateTime.getMonth() + 1;
var yyyy = dateTime.getFullYear();
var date = #{date_format};
emit(date, { count: 1 });
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment