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

Distinct command result set may include or exclude null/undefined depending on presence of index

    XMLWordPrintableJSON

Details

    • Query Optimization
    • ALL
    • Hide

      db.test.drop();
      db.test.insert({ field: [] });
       
      // With no index:
      db.test.distinct('field');
      // =>  [ ]
       
      // With an index:
      db.test.ensureIndex({ field: 1 });
      db.test.distinct('field');
      // =>  [ null ]

      And now with an additional document:

      db.test.drop();
      db.test.insert({ field: [] });
      db.test.insert({ field: [1, 2] });
       
      // With no index:
      db.test.dropIndex({ field: 1 });
      db.test.distinct('field');
      // =>  [ 1, 2 ]
       
      // With an index:
      db.test.ensureIndex({ field: 1 });
      db.test.distinct('field');
      // =>  [ null, 1, 2 ]

      Show
      db.test.drop(); db.test.insert({ field: [] });   // With no index: db.test.distinct('field'); // => [ ]   // With an index: db.test.ensureIndex({ field: 1 }); db.test.distinct('field'); // => [ null ] And now with an additional document: db.test.drop(); db.test.insert({ field: [] }); db.test.insert({ field: [1, 2] });   // With no index: db.test.dropIndex({ field: 1 }); db.test.distinct('field'); // => [ 1, 2 ]   // With an index: db.test.ensureIndex({ field: 1 }); db.test.distinct('field'); // => [ null, 1, 2 ]

    Description

      When distinct is called on a field for which there exists a document with an empty array in said field, it will return null in the result if there is an index on the field.

      Attachments

        Activity

          People

            Votes:
            8 Vote for this issue
            Watchers:
            25 Start watching this issue

            Dates

              Created:
              Updated: