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

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

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Minor - P4
    • Resolution: Fixed
    • 5.2.1
    • 7.2.0.rc1
    • Query
    • None
    • 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.
    • 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

              oleg.pudeyev@mongodb.com Oleg Pudeyev
              CedricBm Cedric Beaumont
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: