Info
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.
Top User Comments
thomas.schubert commented on Sat, 30 Sep 2017 15:49:56 +0000:
Hi s.pantaleev,
Thank you for the detailed report with clear reproduction steps. We're able to reproduce this issue and are tracking it in SERVER-14832. Please feel free to vote for SERVER-14832 and watch it for updates.
Kind regards,
Kelsey
Steps to Reproduce
> 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" ]