title: MongoDB data aggregation assistance
body: <p>I have 'customers' collection with the following document:</p>
  <pre><code>{
    id: 1,
    name: 'Customer Name',
    projects: [
      {
        id: 1000,
        name: 'Project 1',
        description: 'Project description',
        instances: [10, 20],
      },
      {
        id: 2000,
        name: 'Project 2',
        description: 'Project description',
        instances: [30, 40, 10],
      }
    ]
  }
  </code></pre>
  <p>I have another collection 'instances' which looks like the following:</p>
  <pre><code>[
    {
      id: 10,
      operatingSystem: 'Microsoft Windows 2012R2',
      version: '3.1.5',
      product: {
        id: 100,
        name: 'Product 1',
        vendor: 'Vendor A',
      },
    },
    {
      id: 20,
      operatingSystem: 'Microsoft Windows 2016',
      version: '4.1.0',
      product: {
        id: 200,
        name: 'Product 5',
        vendor: 'Vendor B',
      },
    },
    {
      id: 30,
      operatingSystem: 'Microsoft Windows 2019',
      version: '3.0',
      product: {
        id: 300,
        name: 'Product 2',
        vendor: 'Vendor A',
      },
    },
    {
      id: 40,
      operatingSystem: 'Linux',
      version: '1.0',
      product: {
        id: 100,
        name: 'Product 1',
        vendor: 'Vendor A',
      }
    }
  ]
  </code></pre>
  <p>I'm trying to use the aggregation framework to make the results look like the following:</p>
  <pre><code>{
    id: 1,
    name: 'Customer Name',
    projects: [
      {
        id: 1000,
        name: 'Project 1',
        description: 'Project description',
        products: [
          {
            id: 100,
            name: 'Product 1',
            vendor: 'Vendor A',
            instances: [
              {
                id: 10,
                operatingSystem: 'Microsoft Windows 2012R2',
                version: '3.1.5',
              },
            ],
          },
          {
            id: 200,
            name: 'Product 5',
            vendor: 'Vendor B',
            instances: [
              {
                id: 20,
                operatingSystem: 'Microsoft Windows 2016',
                version: '4.1.0',
              },
            ],
          },
        ],
      },
      {
        id: 2000,
        name: 'Project 2',
        description: 'Project description',
        products: [
          {
            id: 300,
            name: 'Product 2',
            vendor: 'Vendor A',
            instances: {
              id: 30,
              operatingSystem: 'Microsoft Windows 2019',
              version: '3.0',
            },
          },
          {
            id: 100,
            name: 'Product 1',
            vendor: 'Vendor A',
            instances: [
              {
                id: 40,
                operatingSystem: 'Linux',
                version: '1.0',
              },
              {
                id: 10,
                operatingSystem: 'Microsoft Windows 2012R2',
                version: '3.1.5',
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>The current pipeline I managed to build is:</p>
  <pre><code>[{$match: {
    _id: 1
  }}, {$unwind: {
    path: &quot;$projects&quot;
  }
  }, {$lookup: {
    from: 'instances',
    localField: 'projects.instances',
    foreignField: '_id',
    as: 'projects.instances'
  }}, {$group: {
    _id: &quot;$projects.instances.product&quot;,
    test: { &quot;$push&quot;: &quot;$$ROOT&quot; }
  }}, {$unwind: {
    path: &quot;$_id&quot;
  }}, {$unwind: {
    path: &quot;$test&quot;
  }}, {$project: {
    _id: &quot;$test._id&quot;,
    name: &quot;$test.name&quot;,
    description: &quot;$test.description&quot;,
    projects: {
      _id: &quot;$test.projects._id&quot;,
      name: &quot;$test.projects.name&quot;,
      description: &quot;$test.projects.description&quot;,
      products: {
        _id: &quot;$_id._id&quot;,
        name: &quot;$_id.name&quot;,
        vendor: &quot;$_id.vendor&quot;,
        instances: &quot;$test.projects.instances&quot;,
      }
    }
  }}, {$group: {
    _id: &quot;$_id&quot;,
    name: {&quot;$first&quot;: &quot;$name&quot;},
    projects: {
      &quot;$push&quot;: &quot;$projects&quot;
    }
  }}]
  </code></pre>
  <p>But I'm getting duplicates in the 'projects' array (if I'm having the same project with a different product, it will show twice instead of having a single project with 2 products in the products array</p>
  <p>Will appreciate your help with finding the right pipeline stages to manipulate my results are expected</p>