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

Query embedded documents by date field with $gt/gte/lt/lte does not find existing documents

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.2.1
    • Affects Version/s: 7.2.0
    • Component/s: Query
    • Labels:
      None
    • Environment:
      OS: macOS Big Sur 11.0.1
      Ruby: 2.7.2 (MRI)
      Mongo: 4.2
      Mongo Driver: 2.13.2
    • Fully Compatible

      After updating Mongoid to 7.2.0, I noticed that querying embedded association by date field no longer works in my environment.

      The same code worked after downgrading to Mongoid 7.1.6

      Below show how the models are defined:

      // Some comments here
      class UsagePeriod
        include Mongoid::Document
        include Mongoid::Timestamps
      
        field :used_quantity,           type: Float, default: 0
        field :period_start_date,       type: Date
        field :period_end_date,         type: Date
      end
      
      class Prescription
        include Mongoid::Document
        include Mongoid::Timestamps
      
        # ... 
      
        embeds_many :usage_periods
      end
      

      What I was trying to do:

      prescription.usage_periods.find_by(:period_start_date.gte => Date.current)
      # => It always returns nil no matter  it's period_start_date.gte or period_start_date.lte
      

      Non-date field is working:

      b=Prescription.create!(usage_periods:[UsagePeriod.new(used_quantity:2)])
      
      b.usage_periods.where(:used_quantity.ne => 1).first
      
      => #<UsagePeriod _id: 5ff609a54896684978f729d4, created_at: nil, updated_at: nil, used_quantity: 2.0, period_start_date: nil, period_end_date: nil>
      

      Date field is not working:

      a=Prescription.create!(usage_periods:[UsagePeriod.new(period_start_date:Date.new(2020,12,22))])
      
      a.usage_periods.where(:period_start_date.gte => Date.new(2020,12,21)).first
      => nil
      

      Date field with exact match is working:

      a.usage_periods.where(:period_start_date => Date.new(2020,12,22)).first
      => #<UsagePeriod _id: 5ff6091a4896684978f729d2, created_at: nil, updated_at: nil, used_quantity: 0.0, period_start_date: 2020-12-22 00:00:00 UTC, period_end_date: nil>
      
      

      Adding an operator makes it not work:

      a.usage_periods.where(:period_start_date.gte => Date.new(2020,12,22)).first
      => nil
      

      Using mql syntax still is not working, generated query appears to be correct;

      irb(main):055:0> a.usage_periods.where(:period_start_date => {'$gte'=>Date.new(2020,12,22)}).first
      => nil
      irb(main):056:0> a.usage_periods.where(:period_start_date => {'$gte'=>Date.new(2020,12,22)})
      => 
      #<Mongoid::Criteria
        selector: {"period_start_date"=>{"$gte"=>2020-12-22 00:00:00 UTC}}
        options:  {}
        class:    UsagePeriod
        embedded: true>
      
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            dingxizheng@cannalogue.ca Xizheng Ding
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: