title: MongoDB Aggregation: How to $match after a $lookup?
body: <p>I have two collections: <code>collA</code> and <code>collB</code> and I'm doing the following lookup:</p>
  <pre><code>db.collA.aggregate([
  {
      $lookup: {from: 'collB', localField: '_id', foreignField: 'key', as: 'bs'}
  }])
  </code></pre>
  <p>If we stop here then <code>bs</code> is an array of objects</p>
  <p>Every &quot;b&quot; object has a field <code>name</code>.</p>
  <p>I'd like to filter only records where one of the <code>bs</code> answers the condition: <code>b.name == query_name</code>.</p>
  <ol>
  <li>I think <code>$elemMatch</code> isn't possible</li>
  <li>I also think I can <code>$unwind</code> and <code>$group</code> but it feels like an overhead.</li>
  </ol>
  <p>Is there a simple way to filter by this condition?</p>
  <p><strong>Sample Data:</strong></p>
  <p>collA:</p>
  <pre><code>[
      {&quot;_id&quot;: &quot;1&quot;, &quot;a&quot;: 1, &quot;b&quot;: 1},
      {&quot;_id&quot;: &quot;2&quot;, &quot;a&quot;: 2, &quot;b&quot;: 2}
  ]
  </code></pre>
  <p>collB:</p>
  <pre><code>[
      {&quot;key&quot;: &quot;1&quot;, &quot;name&quot;: &quot;Ron&quot;},
      {&quot;key&quot;: &quot;1&quot;, &quot;name&quot;: &quot;Bob&quot;},
      {&quot;key&quot;: &quot;1&quot;, &quot;name&quot;: &quot;Dana&quot;},
      {&quot;key&quot;: &quot;2&quot;, &quot;name&quot;: &quot;John&quot;},
      {&quot;key&quot;: &quot;2&quot;, &quot;name&quot;: &quot;Ron&quot;}
  ]
  </code></pre>
  <p>So if the query is <code>Ron</code> I'm expecting to get both documents from collA. If the query is <code>Bob</code> I'm expecting to get only the document with <code>_id == 1</code>.</p>