title: mongodb - merge nested object array into one keeping all data
body: <p>I have 2 collections in mongodb 4.4.0.
  I have a video collection that contains all the videos and some related fields, and a collection called users where I store all the users and a list of videos they have completed, including the notes they have placed on the individual video.
  Now I have to get a list of all the videos, combined with the fields of the videos contained in the user collection, only obviously in cases where there is correspondence</p>
  <p>Videos</p>
  <pre><code>{
    &quot;_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa01&quot;),
    &quot;title&quot;: &quot;Video title 1&quot;,
    &quot;duration&quot; : 120,
    &quot;author&quot; : &quot;John Doe&quot;
  },
  {
    &quot;_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa02&quot;),
    &quot;title&quot;: &quot;Video title 2&quot;,
    &quot;duration&quot; : 180,
    &quot;author&quot; : &quot;Maria Hernandez&quot;
  },
  {
    &quot;_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa03&quot;),
    &quot;title&quot;: &quot;Video title 3&quot;,
    &quot;duration&quot; : 75,
    &quot;author&quot; : &quot;Henry Ford&quot;
  }
  </code></pre>
  <p>Users (only one for demo purpose)</p>
  <pre><code>{
  &quot;_id&quot;: ObjectId(&quot;bbbbbbbbbbbbbbbbbbbbbb01&quot;),
  &quot;fullname&quot;: &quot;James Smith&quot;,
  &quot;videos&quot;:
   [
    {
      &quot;video_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa01&quot;),
      &quot;views&quot;: 12,
      &quot;completed&quot;: true,
      &quot;notes&quot;: &quot;very fun video&quot;,
      &quot;tags&quot;: [&quot;fun&quot;, &quot;best&quot;]
    },
    {
      &quot;video_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa02&quot;),
      &quot;views&quot;: 64,
      &quot;completed&quot;: false,
      &quot;notes&quot;: &quot;very difficult to understand&quot;,
      &quot;tags&quot;: [&quot;hard&quot;, &quot;training&quot;, &quot;sport&quot;]
    }
   ]
  }
  </code></pre>
  <p>Desired result merged objects, assuming I'm filtering for user bbbbbbbbbbbbbbbbbbbbbb01:</p>
  <pre><code>{
      &quot;_id&quot;: ObjectId(&quot;bbbbbbbbbbbbbbbbbbbbbb01&quot;),
      &quot;fullname&quot;: &quot;James Smith&quot;,
      &quot;videos&quot;:
       [
        {
          &quot;video_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa01&quot;),
          &quot;title&quot;: &quot;Video title 1&quot;,
          &quot;duration&quot; : 120,
          &quot;author&quot; : &quot;John Doe&quot;
          &quot;views&quot;: 12,
          &quot;completed&quot;: true,
          &quot;notes&quot;: &quot;very fun video&quot;,
          &quot;tags&quot;: [&quot;fun&quot;, &quot;best&quot;]
        },
        {
          &quot;video_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa02&quot;),
          &quot;title&quot;: &quot;Video title 2&quot;,
          &quot;duration&quot; : 180,
          &quot;author&quot; : &quot;Maria Hernandez&quot;
          &quot;views&quot;: 64,
          &quot;completed&quot;: false,
          &quot;notes&quot;: &quot;very difficult to understand&quot;,
          &quot;tags&quot;: [&quot;hard&quot;, &quot;training&quot;, &quot;sport&quot;]
        },
        {
          &quot;video_id&quot;: ObjectId(&quot;aaaaaaaaaaaaaaaaaaaaaa03&quot;),
          &quot;title&quot;: &quot;Video title 3&quot;,
          &quot;duration&quot; : 75,
          &quot;author&quot; : &quot;Henry Ford&quot;
        }
       ]
      }
  </code></pre>
  <p>I searched a lot on google and SO for example:
  <a href="https://stackoverflow.com/questions/64035829/mongodb-lookup-in-complex-nested-array">MongoDB - $lookup in complex nested array</a> or
  <a href="https://stackoverflow.com/questions/53962412/merge-two-array-objects-together-in-mongodb-from-lookup">Merge two array objects together in Mongodb from $Lookup</a></p>
  <p>I'm trying to use $ lookup and $ mergeObjects but I just can't get the result I want.</p>