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 }
- is duplicated by
-
SERVER-17221 Query on key-val array using $all and $elemMatch does not seem to correctly use indexes
- Closed
- related to
-
SERVER-19999 $all with $elemMatch indexes first element regardless of predicate cardinality (regression)
- Closed