title: Mongo using the wrong index during aggregation with match+sort operations
body: <p>I'm using MongoDB version <code>4.2.0</code>. I have a collection with the following indexes:</p>
  
  <pre class="lang-js prettyprint-override"><code>{uuid: 1},
  {unique: true, name: "uuid_idx"}
  </code></pre>
  
  <p>and</p>
  
  <pre class="lang-js prettyprint-override"><code>{field1: 1, field2: 1, _id: 1},
  {unique: true, name: "compound_idx"}
  </code></pre>
  
  <p>When executing this query</p>
  
  <pre class="lang-js prettyprint-override"><code>aggregate([
    {"$match": {"uuid": &lt;uuid_value&gt;}}
  ])
  </code></pre>
  
  <p>the planner correctly selects <code>uuid_idx</code>.</p>
  
  <p>When adding this sort clause</p>
  
  <pre class="lang-js prettyprint-override"><code>aggregate([
    {"$match": {"uuid": &lt;uuid_value&gt;}},
    {"$sort": {"field1": 1, "field2": 1, "_id": 1}}
  ])
  </code></pre>
  
  <p>the planner selects <code>compound_idx</code>, which makes the query slower.</p>
  
  <p>I would expect the sort clause to not make a difference in this context. <strong>Why does Mongo not use the <code>uuid_idx</code> index in both cases</strong>?</p>
  
  <p><strong>EDIT</strong>:
  A little clarification, I understand there are workarounds to use the correct index, but I'm looking for an explanation of why this does not happen automatically (if possible with links to the official documentation). Thanks!</p>