title: MongoDB aggregation $lookup to a field that is an indexed array
body: <p>I am trying a fairly complex aggregate command on two collections involving <code>$lookup</code> pipeline. This normally works just fine on simple aggregation as long as index is set on <code>foreignField</code>.</p>
  
  <p>But my <code>$lookup</code> is more complex as the indexed field is not just a normal <code>Int64</code> field but actually an array of <code>Int64</code>. When doing a simple <code>find()</code>, it is easy to verify using <code>explain()</code> that the index is being used. But explaining the aggregate pipeline does not explain whether index is being used in the <code>$lookup</code> pipeline. All my timing tests seem to indicate that the index is not being used. MongoDB version is 3.6.2. Db compatibility is set to 3.6.</p>
  
  <p>As I said earlier, I am not using simple <code>foreignField</code> lookup but the 3.6-specific <code>pipeline</code> + <code>$match</code> + <code>$expr</code>...</p>
  
  <p>Could using <code>pipeline</code> be showstopper for the index? Does anyone have any deep experience with the new <code>$lookup</code> pipeline syntax and/or the index on an array field?</p>
  
  <p><strong>Examples</strong></p>
  
  <p>Either of the following works fine and if explained, shows that index on <code>followers</code> is being used.</p>
  
  <pre><code>db.col1.find({followers: {$eq : 823778}})
  db.col1.find({followers: {$in : [823778]}})
  </code></pre>
  
  <p>But the following one does not seem to make use of the index on <code>followers</code> [there are more steps in the pipeline, stripped for readability].</p>
  
  <pre><code>db.col2.aggregate([
      {$match:{field: "123"}},
      {$lookup:{
         from: "col1",
         let : {follower : "$follower"},
         pipeline: [{
              $match: {
                  $expr: {
                      $or: [
                          { $eq : ["$follower", "$$follower"] },                       
                          { $in : ["$$follower", "$followers"]}
                         ]
                  }                        
              }
          }],
         as: "followers_all"
       }
  }])
  </code></pre>