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

Indexes on array fields that contain subarrays does not include some results

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.1.0-rc0
    • Affects Version/s: None
    • Component/s: Query Planning
    • None
    • Query Optimization
    • Fully Compatible
    • ALL
    • v8.0, v7.0, v6.0
    • 200

      Consider the following example:

      // tidx has index on {"array":1}
      > db.tidx.find()
      { "_id" : "A", "array" : [ [ 1 ], 1 ] }
      { "_id" : "B", "array" : [ true ] }
      { "_id" : "C", "array" : [ "b", true ] }
      { "_id" : "D", "array" : [ true, "c" ] }
      { "_id" : "E", "array" : [ [ 1 ], "d", true ] }
      { "_id" : "F", "array" : [ [ 1 ], "e", [ 1 ] ] }
      > db.tidx.aggregate({"$match": {"array": {"$gte": [[1]]}}}) // indexBounds: "array": [ [ [1] ], MaxKey ]
      { "_id" : "B", "array" : [ true ] }
      { "_id" : "D", "array" : [ true, "c" ] }
      { "_id" : "E", "array" : [ [ 1 ], "d", true ] }
      
      // ntidx does not have index on {"array": 1}
      > db.ntidx.find()
      { "_id" : "A", "array" : [ [ 1 ], 1 ] }
      { "_id" : "B", "array" : [ true ] }
      { "_id" : "C", "array" : [ "b", true ] }
      { "_id" : "D", "array" : [ true, "c" ] }
      { "_id" : "E", "array" : [ [ 1 ], "d", true ] }
      { "_id" : "F", "array" : [ [ 1 ], "e", [ 1 ] ] }
      > db.ntidx.aggregate({"$match": {"array": {"$gte": [[1]]}}})
      { "_id" : "A", "array" : [ [ 1 ], 1 ] }
      { "_id" : "B", "array" : [ true ] }
      { "_id" : "D", "array" : [ true, "c" ] }
      { "_id" : "E", "array" : [ [ 1 ], "d", true ] }
      { "_id" : "F", "array" : [ [ 1 ], "e", [ 1 ] ] }
      
      

      In the case with the index we are missing docs with _id A and F in the result set. These should match as per the comparison semantics defined in https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order

            Assignee:
            c.alonso@mongodb.com Carlos Alonso Pérez
            Reporter:
            naafiyan.ahmed@mongodb.com Naafiyan Ahmed
            Votes:
            0 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated:
              Resolved: