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

Distinct on sub-documents array doesn't use index

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.2.4
    • Component/s: Index Maintenance
    • None
    • ALL
      1. Create doc with sub-docs array
      2. Create an index on one of the sub-docs fields
      3. Distinct on that field

      If there is an index on a field in an array of sub-documents, distinct on this field doesn't use the index.

      For example, I have the following collection schema:

      {
          "arr" : [ 
              {
                  "field_1" : 1,
                  "field_2" : 2
              }, 
              {
                  "field_1" : 11,
                  "field_2" : 22
              }
          ]
      }
      

      And I have the following index:

      {
          "arr.field_1" : 1
      }
      

      If I run the following command:

      db.runCommand(
          {
              distinct: 'test',
              key: 'arr.field_1'
          }
      )
      

      I can see that the planSummary uses COLLSCAN and not DISTINCT_SCAN as I expected.

            Assignee:
            Unassigned Unassigned
            Reporter:
            tomg1988@gmail.com Tom Grossman
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: