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

Distinct over empty indexed arrays produces undefined value in the result

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.9
    • Component/s: Querying
    • None
    • ALL
    • Hide
      > db.testcol.insert({"someArray": ["1"]});
      WriteResult({ "nInserted" : 1 })
      > db.testcol.insert({"someArray": ["2"]});
      WriteResult({ "nInserted" : 1 })
      > db.testcol.insert({"someArray": []});
      WriteResult({ "nInserted" : 1 })
      > 
      > db.testcol.distinct("someArray")
      [ "1", "2" ]
      >
      > 
      > db.testcol.ensureIndex({"someArray": 1})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > 
      > db.testcol.distinct("someArray")
      [ undefined, "1", "2" ]
      
      Show
      > db.testcol.insert({"someArray": ["1"]}); WriteResult({ "nInserted" : 1 }) > db.testcol.insert({"someArray": ["2"]}); WriteResult({ "nInserted" : 1 }) > db.testcol.insert({"someArray": []}); WriteResult({ "nInserted" : 1 }) > > db.testcol.distinct("someArray") [ "1", "2" ] > > > db.testcol.ensureIndex({"someArray": 1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > > db.testcol.distinct("someArray") [ undefined, "1", "2" ]

      When using the distinct command targeting an indexed array field, which also happens to be empty, an additional `undefined` value is added to the result.

      If there is no index on the field, it's correct - there is no `undefined` value in the result.

            Assignee:
            kelsey.schubert@mongodb.com Kelsey Schubert
            Reporter:
            s.pantaleev Slavi Pantaleev
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: