title: MongoDB $lookup on one document's array of object
body: <p>I have searched online but could not find any match my case. Here is the situation.</p>
  
  <p>I am using aggregate to combine one collection and one document which is from another collection together</p>
  
  <pre><code>restaurants.aggregate([
    {
      $match: {
        _id: {
          $in: idList
        }
      }
    },
    {
      $lookup: {
        from: "tags",
        localField: "details.restaurantType",
        foreignField: "details.restaurantType._id",
        as: "types"
      }
    },
    {
      $project: {
        restaurantName: "$details.restaurantName",
        restaurantType: "$details.restaurantType",
        type: {
          $filter: {
            input: "$types",
            as: "type",
            cond: {
              $eq: ["$$type._id", "$details.restaurantType"]
            }
          }
        },
        currency: "$details.currency",
        costPerPax: "$details.costPerPax"
      }
    }
  ]);
  </code></pre>
  
  <h3>current result</h3>
  
  <p>The 'type' field in my current result is [], I need a matched value instead</p>
  
  <pre><code>[
      {
          "id": "5c20c7a0036dda80a8baabcc",
          "restaurantName": "Villagio Restaurant Sutera Mall",
          "type": [],
          "currency": "RM",
          "costPerPax": 22,
      },
      {
          "id": "5c20ceb07715216d3c217b7a",
          "restaurantName": "Thai Food Thai Now Sutera Mall",
          "type": [],
          "currency": "RM",
          "costPerPax": 16,
      }
  ]
  </code></pre>
  
  <h3>expected result</h3>
  
  <p>I need the 'type' fields has match tag name from another collection like this</p>
  
  <pre><code>[
      {
          "id": "5c20c7a0036dda80a8baabcc",
          "restaurantName": "Villagio Restaurant Sutera Mall",
          "type": "Western",
          "currency": "RM",
          "costPerPax": 22,
      },
      {
          "id": "5c20ceb07715216d3c217b7a",
          "restaurantName": "Thai Food Thai Now Sutera Mall",
          "type": "Thai",
          "currency": "RM",
          "costPerPax": 16,
      }
  ]
  </code></pre>
  
  <h3>Extra Information</h3>
  
  <p>two document from restaurants collection</p>
  
  <pre><code>    {
          "details": {
              "restaurantName": "Villagio Restaurant Sutera Mall",
              "restaurantType": "5c01fb57497a896d50f498a8"
          },
          "_id": "5c20c7a0036dda80a8baabcc",
          "status": "OP",
          "__v": 0
      },
      {
          "details": {
              "restaurantName": "Kingshahi Japanese Shop",
              "restaurantType": "5c01fb57497a896d50f49879"
          },
          "_id": "5c20cb4fb7e75180480690c2",
          "status": "OP",
          "__v": 0
      } 
  </code></pre>
  
  <p>One document from tag collection</p>
  
  <pre><code>        {
              "_id": "5c01fb57497a896d50f49876",
              "details": {
                  "restaurantTypeId": "5c01fb57497a896d50f49877",
                  "restaurantTypes": [
                      {
                          "_id": "5c01fb57497a896d50f49879",
                          "name": "Asian",
                          "counter": 1
                      },
                      {
                          "_id": "5c01fb57497a896d50f4987a",
                          "name": "Bakery",
                          "counter": 0
                      }
                  ]
              }
          }
  </code></pre>