-
Type: Task
-
Resolution: Won't Fix
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
Would be great to have destructured created_at / updated_at fields. It would be an Array containing the following:
> date = Date.today => Sun, 04 Aug 2013 > destructured_created_at = [ > "#{date.strftime('%Y')}", > "#{date.strftime('%Y%m')}", > "#{date.strftime('%Y')}w#{date.cweek}", > "#{date.strftime('%Y%m%d')}" > ] => ["2013", "201308", "2013w31", "20130804"]
It would be valuable for queries like the following example:
Using default created_at
Find all order created on mondays:
# find all mondays of the year now = Time.now.beginning_or_year now += 1.day until now.monday? mondays = [now] mondays << now += 7.days while now.year == Time.now.year # find all order created on mondays query = { :$or => mondays.map do |day| { :created_at => { :$gte => day.beginning_of_day, :$lte => day.end_of_day } } end }
Performance issue using a $or. What about sorting at the end ? Will skip index on $or
Using destructured created_at
# find all mondays of the year now = Time.now.beginning_or_year now += 1.day until now.monday? mondays = [now] mondays << now += 7.days while now.year == Time.now.year # find all order created on mondays query = { :destructured_created_at => { :$in => mondays.map { |day| day.strftime('%Y%m%d') } } }
Using an index on destructured_created_at would give us nice performance.
What do you guys think ?