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

Implement association `empty?` via `any?` instead of `count`

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.3.3
    • Affects Version/s: None
    • Component/s: Associations
    • Minor Change

      Before a Mongoid::Association::Referenced is loaded into Ruby memory, #exists? and #present? generate queries differently. #exists? (and also #any?) appears to use find one and #present? uses the aggregation pipeline. I would expect that the #exists? behavior (find one) should be used in all cases.

      In addition, similar to MONGOID-5005, sort should be ignored for all of these. (Even if there is a limit/offset, since we're only finding one, sort can be ignored in all cases AFAIK)

      Log examples for reference:

      > f = Franchise.first
      > f.merchant_roles.exists?
      D, 2020-12-10T18:49:25.640910 #24 DEBUG – : MONGODB | foobar-r-shard-00-02.mongodb.net:27017 req:25 conn:1:1 sconn:91912 | foobar.find | STARTED | {"find"=>"merchant_roles", "filter"=>

      {"deleted_at"=>nil, "franchise_id"=>BSON::ObjectId('518404e160008170f78ebe83')}

      , "sort"=>{"franchise_id"=>1, "rank"=>-1, "n"=>1}, "projection"=>{"_id"=>1}, "limit"=>1, "$db"=>"foobar", "$clusterTi...
      D, 2020-12-10T18:49:25.642381 #24 DEBUG – : MONGODB | foobar-r-shard-00-02.mongodb.net:27017 req:25 | foobar.find | SUCCEEDED | 0.001s

      > f.merchant_roles.present?
      D, 2020-12-10T18:49:34.409943 #24 DEBUG – : MONGODB | foobar-r-shard-00-02.mongodb.net:27017 req:26 conn:1:1 sconn:91912 | foobar.aggregate | STARTED | {"aggregate"=>"merchant_roles", "pipeline"=>[{"$match"=>{"deleted_at"=>nil, "franchise_id"=>BSON::ObjectId('518404e160008170f78ebe83')}}, {"$group"=>{"_id"=>1, "n"=>

      {"$sum"=>1}

      }}], "cursor"=>{}, "$db"=>"foobar", "$clusterTime"=>{"clu...
      D, 2020-12-10T18:49:34.412876 #24 DEBUG – : MONGODB | foobar-r-shard-00-02.mongodb.net:27017 req:26 | foobar.aggregate | SUCCEEDED | 0.003s

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: