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

Query Today.end_of_day $lte Tomorrow.beginning_of_day Wrong

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.8
    • Affects Version/s: None
    • Component/s: None

      I'm not sure if this issue is with Mongoid or MongoDB, but these are the results when querying with $lte. The return value should be nil.

      scope :on_date, lambda

      { |date| where(:_id.gte => date.beginning_of_day.utc, :_id.lte => date.end_of_day.utc) }

      1.9.3-p125 :011 > h = MarketHour.on_date(Time.zone.now).first
      MONGODB wall_street_game_production['market_hours'].find({:_id=>{"$gte"=>2012-04-05 04:00:00 UTC, "$lte"=>2012-04-06 03:59:59 UTC}}).limit(-1).sort([[:_id, :asc]])
      => # MarketHour _id: 2012-04-06 04:00:00 UTC, _type: nil, is_open: false, open_time: 2012-04-06 04:00:00 UTC, close_time: 2012-04-06 04:00:00 UTC

      1.9.3-p125 :005 > date = Time.zone.now
      1.9.3-p125 :006 > h = MarketHour.where(:_id.gte => date.beginning_of_day.utc).where(:_id.lte => date.end_of_day.utc).first
      MONGODB wall_street_game_production['market_hours'].find({"$and"=>[{:_id=>{"$gte"=>2012-04-05 04:00:00 UTC}}, {:_id=>{"$lte"=>2012-04-06 03:59:59 UTC}}]}).limit(-1).sort([[:_id, :asc]])
      => # MarketHour _id: 2012-04-06 04:00:00 UTC, _type: nil, is_open: false, open_time: 2012-04-06 04:00:00 UTC, close_time: 2012-04-06 04:00:00 UTC

      I was able to fix this by changing $lte to $lt, however the same query should work with $lte unless I'm mistaken.

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

              Created:
              Updated:
              Resolved: