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

100
Views
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 answers
Answer question

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 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!