2

In a Rails app with mongoid, given a collection with a created_at field, how would you return the number of records created for each week ?

I found somewhere a trick to add

def created_at_week
  created_at.strftime('W%W %Y')
end

so I can do something like .group_by(&:week) but this seems quite slow, I'd rather the database did the count for me instead of parsing everything in memory.

Is there a simple way to do that or do I need a map-reduce/aggregation query ?

I need to generate a table whose rows are the weeks between start_date and end_date, and whose columns are various collection/scopes.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

0 Answers0