title: mongoose aggregate how to map multiple collections into one Array
body: <p>I have four different collections. From which three are connected to one:</p>
  <pre><code>Collection_A = {
      _id: 1
      name: A
      includes: [
          {
              _id: 1,
              includes_id: 222,
          },
          {
              _id: 2,
              includes_id: 333
          }
      ] 
  }
  
  Collection_B = {
      _id: 222,
      type: Computer,
      name: Computer,
      ref_id: 1
  }
  
  Collection_C = {
      _id: 333,
      type: Human,
      name: Human,
      ref_id: 1
  }
  
  Collection_D = {
      _id: 444,
      type: Animal,
      name: Animal,
      ref_id: 1
  }
  </code></pre>
  <p>So collection A can include Collection B, C and D in the includes object. It includes minimum one of the collections.</p>
  <p>So in the includes object in Collection A is the includes_id, which is the _id in Collection B, C and D.
  The _id in Collection A is the ref_id in Collection B, C and D.</p>
  <p>What my problem right now is, that aggregate takes only the last mapped collection.</p>
  <p>My code right now is following:</p>
  <pre><code>     Collection_A.aggregate([
          {
            $lookup: {
              from: &quot;collectionb&quot;,
              localField: &quot;includes.includes_id&quot;,
              foreignField: &quot;_id&quot;,
              as: &quot;colb&quot;,
            },
          },
          {
            $lookup: {
              from: &quot;collectionc&quot;,
              localField: &quot;includes.includes_id&quot;,
              foreignField: &quot;_id&quot;,
              as: &quot;colc&quot;,
            },
          },
          {
            $project: {
              _id: 1,
              status: 1,
              type: 1,
              includes_list: {
                $map: {
                  input: &quot;$includes&quot;,
                  as: &quot;i&quot;,
                  in: {
                    $arrayElemAt: [
                          {
                          $filter: {
                              input: &quot;$colb&quot;,
                              cond: {
                              $eq: [&quot;$$this._id&quot;, &quot;$$i.includes_id&quot;],
                              },
                          },
                          },
                          0,
                      ],
                      $arrayElemAt: [
                          {
                          $filter: {
                              input: &quot;$colc&quot;,
                              cond: {
                              $eq: [&quot;$$this._id&quot;, &quot;$$i.includes_id&quot;],
                              },
                          },
                          },
                          0,
                      ],
                  },
                },
              },
            },
          },
        ]);
  
  </code></pre>
  <p>I tried to make the $lookup as the same on every lookup, but so it only took the last looked up data, and the others where shown as null.
  So I made $lookup as unique, and put two ins in map, but then also the last looked up data was shown, and the others where null.</p>
  <p>When I do the mapping like that:</p>
  <pre><code>includes_list: {
      $map: {
      input: &quot;$icludes&quot;,
      as: &quot;i&quot;,
      in: {
          {
              Col_A : {
              $arrayElemAt: [
                  {
                      $filter: {
                          input: &quot;$A&quot;,
                          cond: {
                          $eq: [&quot;$$this._id&quot;, &quot;$$i.includes&quot;],
                          },
                      },
                  },
                  0,
              ],
              },
              Col_B : {
              $arrayElemAt: [
                  {
                      $filter: {
                          input: &quot;$B&quot;,
                          cond: {
                          $eq: [&quot;$$this._id&quot;, &quot;$$i.includes&quot;],
                          },
                      },
                  },
                  0,
              ],
              }
          }
      },
      },
  }
  </code></pre>
  <p>It workes. But not with the right output, because I need includes_list within one array.</p>
  <p>My desired output is like following:</p>
  <pre><code>{
      includes: [
          {
              _id: 1,
              name: Computer,
              includes_list: [
                  {
                      _id: 222,
                      type: Computer,
                      name: Computer,
                      ref_id: 1
                  },
                  {
                      _id: 333,
                      type: Human,
                      name: Human,
                      ref_id: 1
                  },
              ]
          },
          {
              _id: 2,
              name: Animal,
              includes_list: [
                  {
                      _id: 333,
                      type: Human,
                      name: Human,
                      ref_id: 2
                  },
              ]
          }
      ]
  }
  </code></pre>
  <p>Would appreciate any help!</p>