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

.in Range expansion should use $lte/$gte where possible

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: Performance
    • Labels:

      This is from the current docs. I've tested and confirmed its the current behavior as of 7.5:

      Query method .in expands ``Array``-compatible types, such as a ``Range``,
      when they are used with these operator methods:
      
        Band.in(year: 1950..1960)
        => #<Mongoid::Criteria
          selector: {"year"=>{"$in"=>[1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960]}}
      

      Consider Civilization.in(year: -10000..2000) you're gonna get a really large range in Ruby memory, and transmit over the wire to the DB. Not ideal.

      Instead, it would be ideal if we can use $lte/$gte operators for numeric and date types, same as do in .where method:

        Band.in(year: 1950..1960)
        => #<Mongoid::Criteria
          selector: {"year"=>{"$lte"=>1950, "$gte"=>1960}}
      

      Some types would need to retain array conversion, for example "a".."c" => ["a", "b", "c"] (though $lte/$gte may actually work in the DB, I haven't tested.)

      Alternatively, we could deprecate this behavior and have users just use .where(1..3) instead. But I do think it's nice to have it in .in

            Assignee:
            Unassigned Unassigned
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: