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

104
Vistas
Mongodb aggregation. I need to filter results that matches within multiple arrays

I have this one query that takes to $in parameter different arrays. One is openBehaviourIds, second is closeBehaviours. I would like to avoid duplicated code and make one query with aggregation to get result as {openBehaviours: [{}, {}...], closeBehaviours: [{},{},{}...]} istead of using:

  1. const openBehaviours = await behavioursCollection.aggregate([...])
  2. const closeBehaviours = await behavioursCollection.aggregate([...])

open and close behaviourIds are arrays of strings

Full query for to get both behaviours below (almost the same):

const openBehaviours = await behavioursCollection.aggregate([
            { $match: { 'behaviourId': { $in: openBehaviourIds } } }, {
                $lookup: {
                    from: 'colors',
                    localField: 'domainId',
                    foreignField: 'domainId',
                    as: 'color'
                }
            }, { $unwind: '$color' }
        ], { session }).toArray()

const closeBehaviours = await behavioursCollection.aggregate([
            { $match: { 'behaviourId': { $in: closeBehaviours } } }, {
                $lookup: {
                    from: 'colors',
                    localField: 'domainId',
                    foreignField: 'domainId',
                    as: 'color'
                }
            }, { $unwind: '$color' }
        ], { session }).toArray()

How could I merge this to one query to make it crystal clear? Thanks for help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use $facet operator to achieve that.

behavioursCollection.aggregate([
{$facet:{
    "openBehaviours":[
        { $match: { 'behaviourId': { $in: openBehaviourIds } } },
        {
            $lookup: {
                from: 'colors',
                localField: 'domainId',
                foreignField: 'domainId',
                as: 'color'
            }
        },
        { $unwind: '$color' }
    ],
    "closeBehaviours":[
        { $match: { 'behaviourId': { $in: closeBehaviours  } } },
        {
            $lookup: {
                from: 'colors',
                localField: 'domainId',
                foreignField: 'domainId',
                as: 'color'
            }
        },
        { $unwind: '$color' }   
    ]
}}]);
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