Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
Mongodb multiple $lookup depends on the first lookup

Hi i have a mongo scheme called "payments" that has a 2 keys that optional: userId or representativeId (if userId exist representativeId not exist and same about representativeId). When i found the payments scheme based on cheque scheme that using the $match to filter the result based on my data, I bring all payments in the first lookup that match for my query , in the second and third lookup I want to bring the user data / representative data. Maybe it will be mix of them its ok, its what I want to see if one of them does not exist the other must to be exist.

I want to get the final array that include the user or the representative or mix of them in the same array.

I am using aggregate to implement this.

the problem its give me back a empty array when 2 of the lookup show(user and representative), but when I comment the lookup of the user or the representative, and i left with 2 lookup one for payment and after that last lookup user / representative and its work me like I want but just for user / representative just if i remove one of the lookup. I want its bring my array with two of them.

const userAndRepData = await ChequeDB.aggregate<{[key: string]: any}>([
      {
          $match: {
              $and: [
                  {
                    'chequeNumber': {
                        $in: chequeData.map(c => c.chequeNumber)
                    },
                    'accountNumber': {
                        $in: chequeData.map(c => c.accountNumber)
                    }
                  }
              ]
            
          }
      },          
      {
          $lookup: { 
              from: 'payments',
              localField: 'paymentId',
              foreignField: '_id',
              as: 'payment'
          }
      },
      {
          $unwind:'$payment'
      },
       {
          $lookup: {
              from: 'representatives',
              localField: 'payment.representativeId',
              foreignField: '_id',
              as: 'representative'
          }
      },
      {
        $unwind: '$representative'
      },
      {
          $lookup: {
              from: 'users',
              localField: 'payment.userId',
              foreignField: '_id',
              as: 'user'
          }
      },
      {
          $unwind: '$user'
      },
      {
        $project: {
            user: 1,
            _id: 0,
             representative: 1
        }
      }
  ]);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Unfortunately, you can't use data from one $lookup in the next one since the data from $lookup still hasn't been loaded at the moment query is executing, i.e. payment.representativeId will be null.

You can write resource intensive queries that would work around this, but the easiest and best (performance-wise) way to execute this query would be to have representativeId and userId stored on the Cheque collection.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!