title: How to unwind nested object arrays in mongodb aggregation?
body: <p>I have some data with an array of objects which contains another array. I am trying to unwind the <strong>courses</strong> array then the <strong>requirements</strong> array to match subjects then group everything back together.</p>
  <p>So far I can work fine with the <strong>courses</strong> array. But I just can't get around doing the same thing for the second array  <strong>requirements</strong>.</p>
  <pre><code>[
    {
      &quot;_id&quot;: 1,
      &quot;name&quot;: &quot;provider name&quot;,
      &quot;courses&quot;: [
        {
          &quot;_id&quot;: 1,
          &quot;name&quot;: &quot;course name&quot;,
          &quot;requirements&quot;: [
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            },
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            }
          ]
        }
      ]
    }
  ]
  </code></pre>
  <p>First I unwind both arrays</p>
  <pre><code>  {
      $unwind: {
        path: '$courses',
        preserveNullAndEmptyArrays: true
      }
    },
    {
      $unwind: {
        path: '$courses.requirements',
        preserveNullAndEmptyArrays: true
      }
    },
  </code></pre>
  <p>How I group the first array</p>
  <pre><code>  {
      $group: {
        _id: '$_id',
        root: { $mergeObjects: '$$ROOT' },
        courses: { $push: '$courses' }
      }
    },
    {
      $replaceWith: {
        $mergeObjects: ['$root', '$$ROOT'],
      }
    },
    {
      $project: { root: 0 }
    },
  </code></pre>
  <p>Expected Output</p>
  <pre><code>[
    {
      &quot;_id&quot;: 1,
      &quot;name&quot;: &quot;provider name&quot;,
      &quot;courses&quot;: [
        {
          &quot;_id&quot;: 1,
          &quot;name&quot;: &quot;course name&quot;,
          &quot;requirements&quot;: [
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            },
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            }
          ]
        }
      ]
    }
  ]
  </code></pre>
  <p>But I get</p>
  <pre><code>[
    {
      &quot;_id&quot;: 1,
      &quot;name&quot;: &quot;provider name&quot;,
      &quot;courses&quot;: [
        {
          &quot;_id&quot;: 1,
          &quot;name&quot;: &quot;course name&quot;,
          &quot;requirements&quot;: [
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            },
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            }
          ]
        },
        {
          &quot;_id&quot;: 1,
          &quot;name&quot;: &quot;course name&quot;,
          &quot;requirements&quot;: [
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            },
            {
              &quot;subjects&quot;: [
                &quot;SubjectA::B&quot;,
                &quot;SubjectB::B&quot;,
                &quot;SubjectC::B&quot;
              ]
            }
          ]
        }
      ]
    }
  ]
  </code></pre>