Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-2961

Zero & negative limits not emulated correctly by QueryCache

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.18.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Issue discovered in 7.0.11 but also exists in main (in a slightly different form)

      QueryCache doesn't handle a limit of 0 correctly.Mongodb itself ignores 0 as a limit,

       
      >> Model.collection.find({}, { limit: 1 }).to_a.length
      => 1
      >> Model.collection.find({}, { limit: 0 }).to_a.length
      => 1993
      But when QueryCache is turned on, it is handled incorrectly
      >> Model.collection.find({}, { limit: 1 }).to_a.length
      => 1
      >> Model.collection.find({}, { limit: 0 }).to_a.length
      => 0
      This appears to be due to the following code:

      https://github.com/mongodb/mongoid/blob/master/lib/mongoid/query_cache.rb#L259

      @cursor.to_a[0...limit]

      as this will always return 0 items.

      MongoDB manual says:

      A limit() value of 0 (i.e. .limit(0)) is equivalent to setting no limit.

      so the QueryCache should also handle it in the same way and return everything

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            michael@brightbits.co.uk Michael Baldry
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: