title: MongoDB - Rewind an $unwind nested array after $lookup using $group
body: <p>MongoDB aggregation gets exponentially complicated by the minute!</p>
  
  <p>I am in so far as to <code>$unwind</code> a nested array and then perform a <code>$lookup</code> by the <code>_id</code> of each object from the unwinded nested array. My final attempt is to reverse the unwinding with <code>$group</code>. However, I am unable to reconstruct the original embedded array, with its original property name, along with the rest of the original immediate properties of each document.</p>
  
  <p>Here is my attempt so far:</p>
  
  <pre><code>db.users.aggregate([
      {
          $unwind: "$profile",
          $unwind: {
              path: "$profile.universities",
              preserveNullAndEmptyArrays: true
          }
      },
      {
          $lookup: {
              from: "universities",
              localField: "profile.universities._id",
              foreignField: "_id",
              as: "profile.universities"
          }
      },
      {
          $group: {
              _id: "$_id",
              emails: { "$first": "$emails" },
              profile: { "$first": "$profile" },
              universities: { "$push": "$profile.universities" }
          }
      }
  ]).pretty()
  </code></pre>
  
  <p>What I get is something like this:</p>
  
  <pre><code>{
      "_id" : "A_USER_ID",
      "emails" : [
          {
              "address" : "AN_EMAIL_ADDRESS",
              "verified" : false
          }
      ],
      "profile" : {
          "name" : "NAME",
          "company" : "A COMPANY",
          "title" : "A TITLE",
          "phone" : "123-123-1234",
          "disabled" : false,
          "universities" : [
              {
                  "_id" : "ID_1",
                  "name" : "UNIVERSITY_NAME_1",
                  "code" : "CODE_1",
                  "styles" : {AN_OBJECT}
              }
          ]
      },
      "universities" : [
          [
              {
                  "_id" : "ID_1",
                  "name" : "UNIVERSITY_NAME_1",
                  "code" : "CODE_1",
                  "styles" : {AN_OBJECT}
              }
          ],
          [
              {
                  "_id" : "ID_2",
                  "name" : "UNIVERSITY_NAME_2",
                  "code" : "CODE_2",
                  "styles" : {AN_OBJECT}
              }
          ]
      ]
  }
  </code></pre>
  
  <p>There are 2 issues with this result:</p>
  
  <ol>
  <li>The resulting <code>universities</code> is an array of arrays of one object each, since the <code>$lookup</code> returned a single element array for the original <code>$profile.universities</code> nested array. It should be just an array of objects. </li>
  <li>The resulting <code>universities</code> should take its original place as nested under <code>profiles</code>. I am aware why the original <code>profile.universities</code> is the way it is, because I am using the <code>$first</code> operator. My intent behind this is to retain all of the original properties of <code>profile</code>, in junction with retaining the original nested <code>universities</code> array.</li>
  </ol>
  
  <p>Ultimately, what I need is something like this:</p>
  
  <pre><code>{
      "_id" : "A_USER_ID",
      "emails" : [
          {
              "address" : "AN_EMAIL_ADDRESS",
              "verified" : false
          }
      ],
      "profile" : {
          "name" : "NAME",
          "company" : "A COMPANY",
          "title" : "A TITLE",
          "phone" : "123-123-1234",
          "disabled" : false,
          "universities" : [
              {
                  "_id" : "ID_1",
                  "name" : "UNIVERSITY_NAME_1",
                  "code" : "CODE_1",
                  "styles" : {AN_OBJECT}
              },
              {
                  "_id" : "ID_2",
                  "name" : "UNIVERSITY_NAME_2",
                  "code" : "CODE_2",
                  "styles" : {AN_OBJECT}
              }
          ]
      }
  }
  </code></pre>
  
  <p>Is there another operator that I can use instead of <code>$group</code> to achieve this? Or am I understanding the purpose of <code>$group</code> incorrectly?</p>
  
  <p><strong>Edit:</strong> This is the original post, for context:
  <a href="https://stackoverflow.com/questions/39479875/if-mongo-lookup-is-a-left-outer-join-then-how-come-it-excludes-non-matching-do">If Mongo $lookup is a left outer join, then how come it excludes non-matching documents?</a></p>