title: Nested $lookup in mongodb
body: <p>i have the following situation:</p>
  <p>users: have the assign user roles</p>
  <pre><code>{
  &quot;baseData&quot;: {
      &quot;organizations&quot;: [{
          &quot;$oid&quot;: &quot;60cc97caa27b7c6313c5a488&quot;
      }],
      &quot;accounts&quot;: [{
          &quot;_id&quot;: {
              &quot;$oid&quot;: &quot;60cc87803a530000173f6d2e&quot;
          },
          &quot;kind&quot;: &quot;anonymous&quot;,
          &quot;deviceIdentifierID&quot;: &quot;12D170E2-E19D-4DE9-9DD2-86CCC3A63A4F&quot;
      }, {
          &quot;_id&quot;: {
              &quot;$oid&quot;: &quot;60cc87ac3a530000173f6d30&quot;
          },
          &quot;kind&quot;: &quot;internal&quot;,
          &quot;password&quot;: &quot;$2b$12$TjBPxtL.LQbtE9kPk9UOlu5J7/MfpQg2k9YnJk15v4SRP7ZQUOTtS&quot;
      }],
      &quot;roles&quot;: [{
          &quot;roleID&quot;: {
              &quot;$oid&quot;: &quot;60cb6c95abb7ab56d62be97d&quot;
          },
          &quot;resource&quot;: &quot;60cc8716638ae25ee9bc4fcf&quot;,
          &quot;validFrom&quot;: {
              &quot;$date&quot;: &quot;2021-06-18T11:47:25.138Z&quot;
          },
          &quot;validUntil&quot;: {
              &quot;$date&quot;: &quot;2021-06-18T11:47:25.138Z&quot;
          }
      }, {
          &quot;roleID&quot;: {
              &quot;$oid&quot;: &quot;60cb6c59abb7ab56d62be97b&quot;
          },
          &quot;resource&quot;: &quot;60cc8716638ae25ee9bc4fcf&quot;,
          &quot;validFrom&quot;: {
              &quot;$date&quot;: &quot;2021-06-18T11:47:25.219Z&quot;
          },
          &quot;validUntil&quot;: {
              &quot;$date&quot;: &quot;2021-06-18T11:47:25.219Z&quot;
          }
      }, {
          &quot;roleID&quot;: {
              &quot;$oid&quot;: &quot;60cb6c95abb7ab56d62be97d&quot;
          },
          &quot;resource&quot;: &quot;60cc8a04d25826607c2ab2e4&quot;,
          &quot;validFrom&quot;: {
              &quot;$date&quot;: &quot;2021-06-18T11:57:05.953Z&quot;
          },
          &quot;validUntil&quot;: {
              &quot;$date&quot;: &quot;2021-06-18T11:57:05.953Z&quot;
          }
      }],
      &quot;email&quot;: &quot;Emanuel.syxgott@gmail.com&quot;,
      &quot;firstName&quot;: &quot;Emanuel&quot;,
      &quot;lastName&quot;: &quot;Bachmann&quot;,
      &quot;username&quot;: &quot;dulappen&quot;
  },
  &quot;system&quot;: {
      &quot;createdAt&quot;: {
          &quot;$date&quot;: &quot;2021-06-18T11:46:08.525Z&quot;
      },
      &quot;modifiedAt&quot;: {
          &quot;$date&quot;: &quot;2021-06-18T13:31:33.282Z&quot;
      }
  },
  &quot;__v&quot;: 0
  }
  </code></pre>
  <p>organizations: have a array an organization users</p>
  <pre><code>{
  &quot;baseData&quot;: {
      &quot;users&quot;: [{
          &quot;$oid&quot;: &quot;60cc97a4a27b7c6313c5a484&quot;
      }, &quot;60cc87803a530000173f6d2d&quot;],
      &quot;name&quot;: &quot;Goldhaus club&quot;,
      &quot;email&quot;: &quot;goldhaus.club@koram.at&quot;,
      &quot;imageUrl&quot;: null,
      &quot;description&quot;: null
  },
  &quot;system&quot;: {
      &quot;createdAt&quot;: {
          &quot;$date&quot;: &quot;2021-06-18T12:55:38.261Z&quot;
      },
      &quot;modifiedAt&quot;: {
          &quot;$date&quot;: &quot;2021-06-18T13:37:21.699Z&quot;
      }
  },
  &quot;__v&quot;: 0
  }
  </code></pre>
  <p>userRoles: contain all available user roles (e.g. admin role)</p>
  <pre><code>{
  &quot;system&quot;: {
      &quot;createdAt&quot;: {
          &quot;$date&quot;: &quot;2021-06-17T15:36:50.198Z&quot;
      },
      &quot;modifiedAt&quot;: {
          &quot;$date&quot;: &quot;2021-06-17T15:36:50.198Z&quot;
      }
  },
  &quot;permissions&quot;: [],
  &quot;title&quot;: &quot;Organization Admin&quot;,
  &quot;alias&quot;: &quot;ORGANIZATION_ADMIN&quot;,
  &quot;description&quot;: &quot;All Permissions in the Organisation&quot;,
  &quot;__v&quot;: 0
  </code></pre>
  <p>}</p>
  <p>i need a query that gives me the organisation data with the users and there roles</p>
  <p>my aggregate query</p>
  <pre><code>const userPermissions = await Organization.aggregate([
    {
      $match: args.organizationQuery,
    },
  
    {
      $lookup: {
        from: 'users',
        localField: 'baseData.users',
        foreignField: '_id',
        as: 'baseData.users',
      },
    },
  
    {
      $lookup: {
        from: 'userroles',
        localField: 'baseData.users.baseData.roles.roleID',
        foreignField: '_id',
        as: 'rolesFromUsers',
      },
    },
  
    {
      $addFields: {
        'baseData.users.rolesFromUserAlias': '$rolesFromUsers.alias',
      },
    },
  
    {
      $unset: [
        'baseData.users.baseData.organizations',
        'baseData.users.baseData.accounts',
        'baseData.users.system',
        'system',
      ],
    },
  
  ]);
  </code></pre>
  <p>result</p>
  <pre><code>[
      {
          &quot;_id&quot;: &quot;60cc97caa27b7c6313c5a488&quot;,
          &quot;baseData&quot;: {
              &quot;users&quot;: [
                  {
                      &quot;_id&quot;: &quot;60cc87803a530000173f6d2d&quot;,
                      &quot;baseData&quot;: {
                          &quot;roles&quot;: [
                              {
                                  &quot;roleID&quot;: &quot;60cb6c95abb7ab56d62be97d&quot;,
                                  &quot;resource&quot;: &quot;60cc8716638ae25ee9bc4fcf&quot;,
                                  &quot;validFrom&quot;: &quot;2021-06-18T11:47:25.138Z&quot;,
                                  &quot;validUntil&quot;: &quot;2021-06-18T11:47:25.138Z&quot;
                              },
                              {
                                  &quot;roleID&quot;: &quot;60cb6c59abb7ab56d62be97b&quot;,
                                  &quot;resource&quot;: &quot;60cc8716638ae25ee9bc4fcf&quot;,
                                  &quot;validFrom&quot;: &quot;2021-06-18T11:47:25.219Z&quot;,
                                  &quot;validUntil&quot;: &quot;2021-06-18T11:47:25.219Z&quot;
                              },
                              {
                                  &quot;roleID&quot;: &quot;60cb6c95abb7ab56d62be97d&quot;,
                                  &quot;resource&quot;: &quot;60cc8a04d25826607c2ab2e4&quot;,
                                  &quot;validFrom&quot;: &quot;2021-06-18T11:57:05.953Z&quot;,
                                  &quot;validUntil&quot;: &quot;2021-06-18T11:57:05.953Z&quot;
                              }
                          ],
                          &quot;email&quot;: &quot;alessandro@vondot.dat&quot;,
                          &quot;firstName&quot;: &quot;luk&quot;,
                          &quot;lastName&quot;: &quot;rhomberg&quot;,
                          &quot;username&quot;: &quot;rhomberg&quot;
                      },
                      &quot;__v&quot;: 0,
                      &quot;rolesFromUserAlias&quot;: [
                          &quot;ORGANIZATION_ADMIN&quot;,
                          &quot;ORGANIZATION_PROMOTION&quot;,
                          &quot;ORGANIZATION_EVENT&quot;
                      ]
                  },
                  {
                      &quot;_id&quot;: &quot;60cc97a4a27b7c6313c5a484&quot;,
                      &quot;baseData&quot;: {
                          &quot;email&quot;: &quot;martin@koram.at&quot;,
                          &quot;firstName&quot;: &quot;Martin&quot;,
                          &quot;lastName&quot;: &quot;Rhomberg&quot;,
                          &quot;username&quot;: &quot;richmen69&quot;,
                          &quot;roles&quot;: [
                              {
                                  &quot;roleID&quot;: &quot;60cb6c12abb7ab56d62be97a&quot;,
                                  &quot;resource&quot;: &quot;60cc97caa27b7c6313c5a488&quot;,
                                  &quot;validFrom&quot;: &quot;2021-06-18T12:55:38.420Z&quot;,
                                  &quot;validUntil&quot;: &quot;1970-01-01T00:00:02.022Z&quot;
                              }
                          ]
                      },
                      &quot;__v&quot;: 0,
                      &quot;rolesFromUserAlias&quot;: [
                          &quot;ORGANIZATION_ADMIN&quot;,
                          &quot;ORGANIZATION_PROMOTION&quot;,
                          &quot;ORGANIZATION_EVENT&quot;
                      ]
                  }
              ],
              &quot;name&quot;: &quot;Goldhaus club&quot;,
              &quot;email&quot;: &quot;goldhaus.club@koram.at&quot;,
              &quot;imageUrl&quot;: null,
              &quot;description&quot;: null
          },
          &quot;__v&quot;: 0,
          &quot;rolesFromUsers&quot;: [
              {
                  &quot;_id&quot;: &quot;60cb6c12abb7ab56d62be97a&quot;,
                  &quot;system&quot;: {
                      &quot;createdAt&quot;: &quot;2021-06-17T15:36:50.198Z&quot;,
                      &quot;modifiedAt&quot;: &quot;2021-06-17T15:36:50.198Z&quot;
                  },
                  &quot;permissions&quot;: [],
                  &quot;title&quot;: &quot;Organization Admin&quot;,
                  &quot;alias&quot;: &quot;ORGANIZATION_ADMIN&quot;,
                  &quot;description&quot;: &quot;All Permissions in the Organisation&quot;,
                  &quot;__v&quot;: 0
              },
              {
                  &quot;_id&quot;: &quot;60cb6c59abb7ab56d62be97b&quot;,
                  &quot;system&quot;: {
                      &quot;createdAt&quot;: &quot;2021-06-17T15:38:01.066Z&quot;,
                      &quot;modifiedAt&quot;: &quot;2021-06-17T15:38:01.066Z&quot;
                  },
                  &quot;permissions&quot;: [],
                  &quot;title&quot;: &quot;Organization Promotion&quot;,
                  &quot;alias&quot;: &quot;ORGANIZATION_PROMOTION&quot;,
                  &quot;description&quot;: &quot;All Permissions for Promotions&quot;,
                  &quot;__v&quot;: 0
              },
              {
                  &quot;_id&quot;: &quot;60cb6c95abb7ab56d62be97d&quot;,
                  &quot;system&quot;: {
                      &quot;createdAt&quot;: &quot;2021-06-17T15:39:01.958Z&quot;,
                      &quot;modifiedAt&quot;: &quot;2021-06-17T15:39:01.958Z&quot;
                  },
                  &quot;permissions&quot;: [],
                  &quot;title&quot;: &quot;Organization Event&quot;,
                  &quot;alias&quot;: &quot;ORGANIZATION_EVENT&quot;,
                  &quot;description&quot;: &quot;All Permissions for Events&quot;,
                  &quot;__v&quot;: 0
              }
          ]
      }
  ]
  </code></pre>
  <p>I only need to know what ROLE ALIAS the users have in the Organisations but i can not assign them correctly to the user.</p>
  <p>Any ideas ?</p>
  <p>Thank you in advance</p>