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

$all clause with elemMatch uses wider bounds than needed

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 2.6.5
    • Fix Version/s: 2.6.10, 2.8.0-rc2
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL
    • Backport Completed:
    • Case:

      Description

      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
      }

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                13 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: