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

130
Vistas
Find/aggregate from another collection based on its _id in mongodb
PetTable {
  _id: ObjectId,
  bdTable: string,
  status: ObjectId(ref=Status)
}

Status {
    _id : ObjectId,
    statusCode : number, --> exple: (1, 2, ...7)
    petId : ObjectId(ref=PetTable),
}

i want to find all objects of PetTable with bdTable = "zzzz" and status =5;

I have no idea how to go about it?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Run an aggregate operation with $lookup pipeline stage as follows:

db.PetTable.aggregate([
  { $match: { bdTable: 'zzzz' } },
  { $lookup: {
    from: 'Status',
    let: { status_id: '$status' },
    pipeline: [
      { $match: {
        $expr: { 
          $and: [
            { $eq: [ 5, '$statusCode' ] },
            { $eq: [ '$_id', '$$status_id' ] }
          ] 
        }
      } }
    ],
    as: 'status'
  } },
  { $match: { 
    $expr: { $gt: [ { $size: '$status' }, 0] }
  } },
]);

Mongo Playground

over 4 years ago · Santiago Trujillo 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