Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

362
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda