title: MongoDB lookup when foreign field is an array of objects
body: <p>I have two collections <code>initiatives</code> and <code>resources</code>: </p>
  
  <p><strong>initiative</strong> document example:</p>
  
  <pre><code>{
      "_id" : ObjectId("5b101caddcab7850a4ba32eb"),
      "name" : "AI4CSR",
      "ressources" : [
          {
              "function" : ObjectId("5c3ddf072430c46dacd75dbb"),
              "participating" : 0.1,
           },
           {
              "function" : ObjectId("5c3ddf072430c46dacd75dbc"),
              "participating" : 5,
           },
           {
              "function" : ObjectId("5c3ddf072430c46dacd75dbb"),
              "participating" : 12,
           },
           {
              "function" : ObjectId("5c3ddf072430c46dacd75dbd"),
              "participating" : 2,
           },
      ],
  }
  </code></pre>
  
  <p>and a <strong>resource</strong> document:</p>
  
  <pre><code>{
      "_id" : ObjectId("5c3ddf072430c46dacd75dbc"),
      "name" : "Statistician",
      "type" : "FUNC",
  }
  </code></pre>
  
  <p>so i want to return each <strong>resource</strong> with the sum of <strong>participating</strong> is have. and to that i need to join the two collection.</p>
  
  <pre><code>db.resources.aggregate([
  {
      "$match": { type: "FUNC" }
  },
  {
      "$lookup": {
              "from": "initiatives",
              "localField": "_id",
              "foreignField": "initiatives.resources",
              "as": "result"
          }
  },
  ])
  </code></pre>
  
  <p>but first i need to <strong>unwind</strong> the foreign field array.</p>
  
  <p>example of the expected output:</p>
  
  <pre><code>{
          "function" : "Data Manager"
          "participation_sum": 50
  }
  {
          "function" : "Statistician"
          "participation_sum": 1.5
  }
  {
          "function" : "Supply Manage"
          "participation_sum": 0
  }
  </code></pre>