Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda