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

Sort order returned by a multikey index scan is inconsistent with the SortKeyGenerator logic

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 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.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            byron.grogan Byron Grogan
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: