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

Unable to perform a covered (indexOnly) query with mongoid

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • None

      I try the same query in the mongo shell and the explain shows index only:

      db.merchants.find({st:{"$regex" : "cr", "$options": "i"}}, {n:1, _id:1})
      { "n" : "Crab shack", "_id" : ObjectId("51c908cc1925f3ca51000001") }
      { "n" : "Tacos R Us", "_id" : ObjectId("51c16f201925f3df4300001b") }
      db.merchants.find({st:{"$regex" : "cr", "$options": "i"}}, {n:1, _id:1}).explain()
      {
      	"cursor" : "BtreeCursor st_1_n_1__id_1 multi",
      	"isMultiKey" : false,
      	"n" : 2,
      	"nscannedObjects" : 2,
      	"nscanned" : 2,
      	"nscannedObjectsAllPlans" : 2,
      	"nscannedAllPlans" : 2,
      	"scanAndOrder" : false,
      	"indexOnly" : true,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"st" : [
      			[
      				"",
      				{
      					
      				}
      			],
      			[
      				/cr/i,
      				/cr/i
      			]
      		],
      		"n" : [
      			[
      				{
      					"$minElement" : 1
      				},
      				{
      					"$maxElement" : 1
      				}
      			]
      		],
      		"_id" : [
      			[
      				{
      					"$minElement" : 1
      				},
      				{
      					"$maxElement" : 1
      				}
      			]
      		]
      	},
      	"server" : "Arvinds-MacBook-Pro.local:27017"
      }
      

      But the same query run from the rails console gives me:

      Merchant.where({:st => { '$regex'=> "cr", '$options' => 'i' } }).only(:_id, :n).explain
      
      => {"cursor"=>"BtreeCursor st_1_n_1__id_1 multi", "isMultiKey"=>false, "n"=>2, "nscannedObjects"=>2, "nscanned"=>2, "nscannedObjectsAllPlans"=>2, "nscannedAllPlans"=>2, "sca
      nAndOrder"=>false, "indexOnly"=>false, "nYields"=>0, "nChunkSkips"=>0, "millis"=>0, "indexBounds"=>{"st"=>[["", {}], [/cr/i, /cr/i]], "n"=>[[{"$minElement"=>1}, {"$maxElemen
      t"=>1}]], "_id"=>[[{"$minElement"=>1}, {"$maxElement"=>1}]]}, "allPlans"=>[{"cursor"=>"BtreeCursor st_1_n_1__id_1 multi", "n"=>2, "nscannedObjects"=>2, "nscanned"=>2, "index
      Bounds"=>{"st"=>[["", {}], [/cr/i, /cr/i]], "n"=>[[{"$minElement"=>1}, {"$maxElement"=>1}]], "_id"=>[[{"$minElement"=>1}, {"$maxElement"=>1}]]}}], "oldPlan"=>{"cursor"=>"Btr
      eeCursor st_1_n_1__id_1 multi", "indexBounds"=>{"st"=>[["", {}], [/cr/i, /cr/i]], "n"=>[[{"$minElement"=>1}, {"$maxElement"=>1}]], "_id"=>[[{"$minElement"=>1}, {"$maxElement
      "=>1}]]}}, "server"=>"Arvinds-MacBook-Pro.local:27017"}
      

      I suspect this is because of the only() modifier. Or is this a bug?

            Assignee:
            Unassigned Unassigned
            Reporter:
            asisin1 asisin1
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: