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

Query may give different results when filtering by $gte: MinKey() depending on the index used and not-existing/null value fields are involved

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 5.0.26, 6.0.15, 7.0.9
    • Component/s: None
    • Labels:
      None
    • Query Execution

      Example:

      db.foo.insertMany([
        { _id: 1, a: 1},
        { _id: 2, a: null},
        { _id: 3, b:1 }
      ])
      
      db.foo.createIndex({"a":1})
      db.foo.aggregate([{$match: { "a": {$gte: MinKey()}}}, {$group: {_id: "$a", c: {$sum:1}}}])
      [ { _id: null, c: 2 }, { _id: 1, c: 1 } ]
      
      db.foo.dropIndexes()
      db.foo.createIndex({ "$**": 1 })
      db.foo.aggregate([{$match: { "a": {$gte: MinKey()}}}, {$group: {_id: "$a", c: {$sum:1}}}])
      [ { _id: null, c: 1 }, { _id: 1, c: 1 } ]
      

      When using classic index filtering by $gte: MinKey() takes into account not existing fields and null value (and therefore the query can be even covered) having { _id: null, c: 2 }

      When using wildcard indexes only the null values are taken into account and not the not-existing fields, therefore the query result is different compared to using classic index { _id: null, c: 1 } and the query cannot be covered.

            Assignee:
            ivan.fefer@mongodb.com Ivan Fefer
            Reporter:
            renato.riccio@mongodb.com Renato Riccio
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: