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

    • Query Optimization
    • ALL
    • Hide
      Unable to find source-code formatter for language: javascript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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:

      Unable to find source-code formatter for language: javascript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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
      Unable to find source-code formatter for language: javascript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml 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: Unable to find source-code formatter for language: javascript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml 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 ]

      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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            mnarayan01 Michael Narayan
            Votes:
            8 Vote for this issue
            Watchers:
            25 Start watching this issue

              Created:
              Updated: