-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
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"=>
, "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"=>
}}], "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