title: MongoDB $lookup with nested object with nested array
body: <p>I have 2 collections.</p>
  
  <p><em>cases</em></p>
  
  <pre><code>_id: ObjectId.
  name: string.
  info: {
      [here can be many different fields with diff types]
      relatedEntities: [
          { role: string;
            id: ObjectId;
          } &lt;--- here can be a lot of entities
      ]
  }
  </code></pre>
  
  <p><em>entities</em></p>
  
  <pre><code>_id: ObjectId.
  type: string,
  name: string,
  info: {
      [here can be many different fields with diff types]
  }
  </code></pre>
  
  <p>I need to retrieve all cases and for each case.info.entities object I need to have field <strong>data</strong> which will equal to entity document ( case.info.entities.id === entity_id)</p>
  
  <p><em>Example what I need to have</em></p>
  
  <pre><code>_id: ObjectId.
  name: string.
  info: {
      [here can be many different fields with diff types]
      entities: [
          { role: string;
            id: ObjectId;
            data: {
              _id: ObjectId.
              type: string,
              name: string,
              info: {
                [here can be many different fields with diff types]
              }
            }
          } &lt;--- here can be a lot of entities
      ]
  }
  </code></pre>
  
  <p>How to do it in a proper way?</p>
  
  <p>At the moment I implemented this is that way:</p>
  
  <pre><code>    { $unwind: "$info.relatedEntities" },
      { $lookup: {
              "from": "entities",
              "localField": "info.relatedEntities.entity",
              "foreignField": "_id",
              "as": "info.relatedEntities.entityObject"
          }},
      { $group: {
          "_id": "$_id",
           "templateType":{$first: "$templateType"},
            "info":{$first: "$info"},
            "relatedEntities": {
                  $push: "$info.relatedEntities"
              }
          }}
  </code></pre>
  
  <p>It's working, but required additional parsing when data is retrieved, but I'd like to do it without workarounds.. </p>