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

Make #any? not retrieve entire result set, like #exists?

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Fixed
    • Affects Version/s: 5.2.1
    • Fix Version/s: 7.2.0.rc1
    • Component/s: Query
    • Labels:
      None
    • Release Notes:
      any? on has_many associations no longer retrieves the entire association from the database. This makes any? more efficient in most cases, but repeated calls to any? when the association is not loaded will query the database on every call.
    • Backwards Compatibility:
      Minor Change

      Description

      I have a simple class:

      class Article
        include Mongoid::Document
      end
      

      Here are some simple queries with the mongo query made:

      Article.criteria.any?
      => {"find"=>"articles", "filter"=>{}}
       
      Article.criteria.exists? # or empty? or blank? or present?
      => {"find"=>"articles", "filter"=>{}, "projection"=>{"_id"=>1}, "limit"=>1}
      

      I couldn't find the reason why any? is not optimized like the other methods. Is there an explanation? And can we improve this?

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              oleg.pudeyev Oleg Pudeyev
              Reporter:
              CedricBm Cedric Beaumont
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: