-
Type: Bug
-
Resolution: Gone away
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.4.10
-
Component/s: Querying
-
None
-
Query
-
ALL
For queries with a predicate over a multikey field and a subsequent sort, the result set is ordered differently depending on whether an index is present. A multikey index scan will only consider keys which fall within the bounds implied by the predicate, but the SortKeyGenerator ignores the bounds on fields other than on the sort pattern.
Consider the following example:
// Insert the following documents. db.foo.insert([ { "_id" : 0, "a" : [ { "b" : 0, "c" : 1 }, { "b" : 1, "c" : 2 } ] }, { "_id" : 1, "a" : [ { "b" : 0, "c" : 2 }, { "b" : 1, "c" : 0 } ] }, { "_id" : 2, "a" : [ { "b" : 0, "c" : 0 }, { "b" : 1, "c" : 1 } ] } ]) // Query predicate is 'a.b' == 0. db.foo.find({ 'a.b': 0 }).sort({ 'a.c': 1 }) { "_id" : 1, "a" : [ { "b" : 0, "c" : 2 }, { "b" : 1, "c" : 0 } ] } { "_id" : 2, "a" : [ { "b" : 0, "c" : 0 }, { "b" : 1, "c" : 1 } ] } { "_id" : 0, "a" : [ { "b" : 0, "c" : 1 }, { "b" : 1, "c" : 2 } ] } // Create compound index. db.foo.createIndex({ 'a.b': 1, 'a.c': 1 }) // Query predicate is 'a.b' == 0. db.foo.find({ 'a.b': 0 }).sort({ 'a.c': 1 }) { "_id" : 2, "a" : [ { "b" : 0, "c" : 0 }, { "b" : 1, "c" : 1 } ] } { "_id" : 0, "a" : [ { "b" : 0, "c" : 1 }, { "b" : 1, "c" : 2 } ] } { "_id" : 1, "a" : [ { "b" : 0, "c" : 2 }, { "b" : 1, "c" : 0 } ] }
The expected result is that the sort order would be the same regardless of whether an index is present.
- is related to
-
SERVER-19402 Change semantics of sorting by array fields in find and aggregate
- Closed