Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3205

Feature: Destructured created_at / updated_at for performance

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • 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 ?

            Assignee:
            Unassigned Unassigned
            Reporter:
            gottfrois gottfrois
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: