title: In MongoDB how to decide for a collection which fields to be indexed for a costly query
body: <p>I have a collection with 1000+ records and I need to run the query below. I have come across the issue that this query takes more than a minute even if the <code>departmentIds</code> array has length something like 15-20. I think if I use an index the query time will be reduced.<br />
  From what I observe the 99% of the time spent on the query is due to the <code>$in</code> part.<br />
  How do I decide which fields to index. Should I index only <code>department.department_id</code> since that's what taking most time or should I create a <strong>compound index</strong> using <code>userId</code>,<code>something</code> and <code>department.department_id</code> (bascially all the fields I'm using in the query here)</p>
  <p>Here is what my query looks like</p>
  <pre class="lang-js prettyprint-override"><code>    let departmentIds = [..........................  can be large]
  
      let query = {
        userId: someid,
        something: something,
        'department.department_id': {
          $in: departmentIds
        }
      };
  
  //db query
     let result =  db
      .collection(TABLE_NAME)
      .find(query)
      .project({
        anotherfield: 1,
        department: 1
      })
      .toArray();
  
  </code></pre>