title: Advanced Mongoose Aggregation
body: <p>So what I currently have is working. But what I want in addition is, that I get in the same query the matched data from Collection C with the currently logged in user.</p>
  <p>So the point is the following: A user can have multiple CollectionA's. And users can join the CollectionA, which is specified in CollectionB.
  In the aggregation I get every joined user. But what I want in addition is, that I can see the currently logged in users data from Collection B.</p>
  <pre><code>CollectionA.aggregate([
      {
        $lookup: {
          from: &quot;collectionB&quot;,
          localField: &quot;_id&quot;,
          foreignField: &quot;col_a_id&quot;,
          as: &quot;colB&quot;,
        },
      },
      {
        $match: {
          &quot;colB.user_id&quot;: mongoose.Types.ObjectId(
            request.user.id
          ),
        },
      },
      {
        $lookup: {
          from: &quot;collectionB&quot;,
          let: { id: &quot;$_id&quot; },
          pipeline: [
            {
              $match: {
                $expr: { $eq: [&quot;$$id&quot;, &quot;$col_a_id&quot;] },
              },
            },
            {
              $lookup: {
                from: &quot;user&quot;,
                localField: &quot;user_id&quot;,
                foreignField: &quot;_id&quot;,
                as: &quot;user&quot;,
              },
            },
            {
              $addFields: {
                x: { $arrayElemAt: [&quot;$x&quot;, 0] },
              },
            },
            {
              $project: {
                param1: &quot;$x.a&quot;,
                param2: &quot;$x.b&quot;,
              },
            },
          ],
          as: &quot;completeList&quot;,
        },
      },
      {
        $project: {
          _id: 1,
          param: 1,
          completeList: &quot;$completeList&quot;,
        },
      },
    ]);
  </code></pre>
  <pre><code>Collection A: 
  
  _id: 11
  param: &quot;fff&quot;
  
  
  Collection B:
  _id: 543
  col_a_id: 11,
  user_id: 789,
  param_I_want: &quot;only from the user who is logged in&quot;
  
  
  User:
  
  _id: 789
  param1: &quot;trfewe&quot;,
  param2: &quot;fewfew&quot;
  </code></pre>
  <p>So my current output is like this:</p>
  <pre><code>[
      {
          &quot;_id&quot;: &quot;11&quot;,
          &quot;param&quot;: &quot;fff&quot;,
          &quot;completeList&quot;: [
              {
                  &quot;_id&quot;: &quot;222&quot;,
                  &quot;user_id&quot;: 789,
                  &quot;col_a_id&quot;: 11,
                  &quot;param1&quot;: &quot;trfewe&quot;,
                  &quot;param2&quot;: &quot;fewfew&quot;,
              },
              {
                  &quot;_id&quot;: &quot;333&quot;,
                  &quot;user_id&quot;: 899,
                  &quot;col_a_id&quot;: 11,
                  &quot;param1&quot;: &quot;fwfer&quot;,
                  &quot;param2&quot;: &quot;gerwa&quot;,
              },
          ]
      }
  ...
  ]
  </code></pre>
  <p>And what I want is this:</p>
  <pre><code>[
      {
          &quot;_id&quot;: &quot;11&quot;,
          &quot;param&quot;: &quot;fff&quot;,
          &quot;completeList&quot;: [
              {
                  &quot;_id&quot;: &quot;222&quot;,
                  &quot;user_id&quot;: 789,
                  &quot;col_a_id&quot;: 11,
                  &quot;param1&quot;: &quot;trfewe&quot;,
                  &quot;param2&quot;: &quot;fewfew&quot;,
              },
              {
                  &quot;_id&quot;: &quot;333&quot;,
                  &quot;user_id&quot;: 899,
                  &quot;col_a_id&quot;: 11,
                  &quot;param1&quot;: &quot;fwfer&quot;,
                  &quot;param2&quot;: &quot;gerwa&quot;,
              },
          ],
          &quot;param_I_want&quot;: &quot;only from the user who is logged in&quot;
      }
  ...
  ]
  </code></pre>
  <p>And since I'm asking a question I add another one into it: In the aggregation I use the same lookup. The second one is in a pipeline.
  Is there a way to do it in a better way? I mean if I imagine it being used by thousands of people, could there be performance issues?
  If yes why and how can I improve it?</p>