Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-16256

$all clause with elemMatch uses wider bounds than needed

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.6.10, 2.8.0-rc2
    • Affects Version/s: 2.6.5
    • Component/s: Querying
    • None
    • ALL

      When using $all with $elemMatch criteria, the query uses wider bounds than what it should have used (v2.4 behaves correctly in selecting narrow bounds). Following are steps indicates the wider bounds being queried on the array field "bc":

      db.x.drop();
      db.x.ensureIndex({a: 1, "dims.b": 1, "dims.c": 1});
      db.x.insert({a: 1, bc: [{b: "abc", c: "xyz"}, {b: "gsh", c: "ysh"]});
      db.x.find({
              "a" : 1,
              "bc" : {
                      "$all" : [
                              {
                                      "$elemMatch" : {
                                              "b" : "pl",
                                              "c" : "tgit"
                                      }
                              },
                              {
                                      "$elemMatch" : {
                                              "b" : "xy",
                                              "c" : "ak"
                                              }
                               }
                      ]
              }
      }).explain();
      

      Explain with $all:

      {
      	"cursor" : "BtreeCursor a_1_bc.b_1_bc.c_1",
      	"isMultiKey" : true,
      	"n" : 0,
      	"nscannedObjects" : 2,
      	"nscanned" : 3,
      	"nscannedObjectsAllPlans" : 2,
      	"nscannedAllPlans" : 3,
      	"scanAndOrder" : false,
      	"indexOnly" : false,
      	"nYields" : 0,
      	"nChunkSkips" : 0,
      	"millis" : 0,
      	"indexBounds" : {
      		"a" : [
      			[
      				1,
      				1
      			]
      		],
      		"bc.b" : [
      			[
      				{
      					"$minElement" : 1
      				},
      				{
      					"$maxElement" : 1
      				}
      			]
      		],
      		"bc.c" : [
      			[
      				{
      					"$minElement" : 1
      				},
      				{
      					"$maxElement" : 1
      				}
      			]
      		]
      	},
      	"filterSet" : false
      }
      

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            anil.kumar Anil Kumar
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: