title: MongoDB Aggregation Lookup Match Array
body: <p>I have two collections, a collection with users and a collection with groups. In <code>groups</code> the documents look like this:</p>
  
  <pre><code>{
      "_id": ObjectId("...")
      "members": [ObjectId("..."), ObjectId("..."), ObjectId("...")]
  }
  </code></pre>
  
  <p>In the <code>users</code> collection the documents look like this:</p>
  
  <pre><code>{
      "_id": ObjectId("...")
      "first_name": "...",
      "last_name": "...",
      "phone": "..."
  }
  </code></pre>
  
  <p>What I want to do is an aggregation in the <code>users</code> collection where I add a <code>$lookup</code> stage to the pipeline where I go inside the <code>groups</code> collection and get the group from the user based on whether the <code>_id</code> from the user document is inside the <code>members</code> array in the  group document.</p>
  
  <p>I have the following but I don't know how I'd proceed.</p>
  
  <pre><code>db.users.aggregate([
      {
          "$match": { "_id": ObjectId("...") }
      },
      {
          "$lookup": {
              "from": "groups",
              "let": {"_id": "$_id"},
              "pipeline": [{"$match": ???}], &lt;- match when $$_id is inside $members
              "as": "group"
      }
  ]);
  </code></pre>
  
  <p>I know that the aggregation framework in MongoDB doesn't work with arrays and that there is an <code>$unwind</code> stage but I don't really know how I would combine <code>$unwind</code> and <code>$match</code> to get the result I want.</p>
  
  <p>What I want:</p>
  
  <pre><code>{
      "_id": ObjectId("..."),
      "group": ObjectId("..."),
      (other fields here)
  }
  </code></pre>
  
  <p>I use MongoDB server version 3.7.2</p>