title: Mongo Distinct Index for Dot Notated
body: <ol>
  <li><p>I have a mongodb v4.2.2 hosted on Atlas. Have a collection
  <code>objects</code> and two fields in it: <code>metadata</code> as array of objects, each
  object has one field <code>key</code> AND second field named as <code>model</code>. Added compound index
  <code>model-&gt;metadata.key</code> as regular one. While running db command
  <code>db.objects.explain('executionStats').distinct('metadata.key',
  {model: ObjectId('5e18aff58a5aaffdc3d6f26d')})</code> from console it
  doesn't use my <code>model-&gt;metadata.key</code> index and doesn't use DISTINCT_SCAN. </p></li>
  <li><p>But if I move data to another collection lets say <code>objectKeys</code> that
  has two fields like <code>key</code>, <code>model</code> and again add regular compound
  index <code>model-&gt;key</code> and run command
  <code>db.objectKeys.explain('executionStats').distinct('key', {model:
  ObjectId('5e18aff58a5aaffdc3d6f26d')})</code> it is successfully applying DISTINCT_SCAN
  and use the index.</p></li>
  <li>Question. How to force MongoDB to use indexes for dot notated
  field during distinct operation?</li>
  </ol>
  
  <p><strong>UPDATE DETAILS:</strong></p>
  
  <p>1.</p>
  
  <pre><code>db.objects.getIndexes();
  {
    "v" : 2,
    "key" : {
      "model" : 1,
      "metadata.key" : 1,
      "metadata.value" : 1
    },
    "name" : "model_1_metadata.key_1_metadata.value_1",
    "ns" : "my_db.objects",
    "background" : true
  }
  
  db.objects.explain('executionStats').distinct('metadata.key', {model: ObjectId('5e18aff58a5aaffdc3d6f26d')})
  
  "executionStages" : {
    "stage" : "FETCH",
      "nReturned" : 0,
      "executionTimeMillisEstimate" : 0,
      "works" : 1,
      "advanced" : 0,
      "needTime" : 0,
      "needYield" : 0,
      "saveState" : 0,
      "restoreState" : 0,
      "isEOF" : 1,
      "docsExamined" : 0,
      "alreadyHasObj" : 0,
      "inputStage" : {
      "stage" : "IXSCAN",
        "nReturned" : 0,
        "executionTimeMillisEstimate" : 0,
        "works" : 1,
        "advanced" : 0,
        "needTime" : 0,
        "needYield" : 0,
        "saveState" : 0,
        "restoreState" : 0,
        "isEOF" : 1,
        "keyPattern" : {
        "model" : 1,
          "metadata.key" : 1,
          "metadata.value" : 1
      },
      "indexName" : "model_1_metadata.key_1_metadata.value_1",
        "isMultiKey" : true,
        "multiKeyPaths" : {
        "model" : [ ],
          "metadata.key" : [
          "metadata"
        ],
          "metadata.value" : [
          "metadata"
        ]
      },
      "isUnique" : false,
        "isSparse" : false,
        "isPartial" : false,
        "indexVersion" : 2,
        "direction" : "forward",
        "indexBounds" : {
        "model" : [
          "[ObjectId('5e18aff58a5aaffdc3d6f26d'), ObjectId('5e18aff58a5aaffdc3d6f26d')]"
        ],
          "metadata.key" : [
          "[MinKey, MaxKey]"
        ],
          "metadata.value" : [
          "[MinKey, MaxKey]"
        ]
      },
      "keysExamined" : 0,
        "seeks" : 1,
        "dupsTested" : 0,
        "dupsDropped" : 0
    }
  }
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>db.object_keys.getIndexes();
  
  {
    "v" : 2,
    "key" : {
      "models" : 1,
      "key" : 1
    },
    "name" : "models_1_key_1",
    "ns" : "my_db.object_keys",
    "background" : true
  },
  
  db.object_keys.explain('executionStats').distinct('key', {models: ObjectId('5e18aff58a5aaffdc3d6f26d')})
  
  "executionStages" : {
    "stage" : "PROJECTION_COVERED",
      "nReturned" : 0,
      "executionTimeMillisEstimate" : 0,
      "works" : 1,
      "advanced" : 0,
      "needTime" : 0,
      "needYield" : 0,
      "saveState" : 0,
      "restoreState" : 0,
      "isEOF" : 1,
      "transformBy" : {
      "_id" : 0,
        "key" : 1
    },
    "inputStage" : {
      "stage" : "DISTINCT_SCAN",
        "nReturned" : 0,
        "executionTimeMillisEstimate" : 0,
        "works" : 1,
        "advanced" : 0,
        "needTime" : 0,
        "needYield" : 0,
        "saveState" : 0,
        "restoreState" : 0,
        "isEOF" : 1,
        "keyPattern" : {
        "models" : 1,
          "key" : 1
      },
      "indexName" : "models_1_key_1",
        "isMultiKey" : true,
        "multiKeyPaths" : {
        "models" : [
          "models"
        ],
          "key" : [ ]
      },
      "isUnique" : false,
        "isSparse" : false,
        "isPartial" : false,
        "indexVersion" : 2,
        "direction" : "forward",
        "indexBounds" : {
        "models" : [
          "[ObjectId('5e18aff58a5aaffdc3d6f26d'), ObjectId('5e18aff58a5aaffdc3d6f26d')]"
        ],
          "key" : [
          "[MinKey, MaxKey]"
        ]
      },
      "keysExamined" : 0
    }
  }
  </code></pre>
  
  <p>So as you can see in the first case it uses IXSCAN means index scan and in the second case it uses DISTINCT_SCAN that is considered more faster. The differences is that in first case the <code>metadata</code> field is array of objects and in the second case the field <code>key</code> is string</p>