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

Inconsistent results from $group with and without index

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 4.1.11
    • Affects Version/s: 4.1.4
    • Component/s: Querying
    • None
    • Fully Compatible
    • ALL
    • Query 2019-05-06, Query 2019-05-20

      > db.d.find()
      { "_id" : ObjectId("5ca4fb6f4e1d8532f8828e79"), "a" : 1 }
      { "_id" : ObjectId("5ca4fb744e1d8532f8828e7a"), "a" : [ 1, 2 ] }
      > db.d.aggregate( [ { $group : { _id : "$a" } } ] ) // query without index
      { "_id" : 1 }
      { "_id" : [ 1, 2 ] }
      > db.d.createIndex({a:1})
      {
          "createdCollectionAutomatically" : false,
          "numIndexesBefore" : 1,
          "numIndexesAfter" : 2,
          "ok" : 1
      }
      
      > db.d.aggregate( [ { $group : { _id : "$a" } } ] ) // query with index
      { "_id" : 1 } // Different from above!
      { "_id" : 2 }
      

      It looks like this is because when an index is present, a DISTINCT_SCAN is used. It seems incorrect to use a DISTINCT_SCAN for a $group when the index is multikey (though we should think about this harder).

            Assignee:
            ian.boros@mongodb.com Ian Boros
            Reporter:
            ian.boros@mongodb.com Ian Boros
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: