Lab $graphLookup with restrictSearchWithMatch

In Chapter 3, last Lab, I tried to figure out the correct solution without looking at the given solution first. My clue is similar to the second choice, i.e. choice B. Even the choice C is the correct method, using method B (choice B) should also work after add more stages at the end. The following is my solution using method B:

db.air_airlines.aggregate([
    {
        $match: {
            'country': {$in: ['Germany', 'Spain', 'Canada']}
        }
    },
    {
        $lookup: {
            from: 'air_alliances',
            let: {local_name: "$name"},
            pipeline: [
                {
                    $match: {name: 'OneWorld', 
                    $expr: {$in: ['$$local_name', '$airlines']}}
                },
            ],
            as: 'alliance'
        }
    },
    {
        $unwind: {
           path: '$alliance',
           preserveNullAndEmptyArrays: false
        }
    },
    {
        $graphLookup: {
            from: 'air_routes',
            startWith: '$base',
            connectFromField: 'dst_airport',
            connectToField: 'src_airport',
            maxDepth: 1,
            depthField: 'layer',
            //restrictSearchWithMatch: {'airline.name': 
            // {$in: [	"Canadian Airlines", "Iberia Airlines", "Air Berlin"]}},
            as: 'connections'
        }
    },
    {
        $group: {
            _id:null,
            validName: {$push: '$name'},
            connections: {$push: '$connections'}
        }
    },
    
    {
        $unwind: '$connections'
    },
    
    {
        $project: {validName: 1, 'connections.airline.name': 1, 
                  'connections.dst_airport': 1, _id: 0}
    },
    
    {
        $unwind: '$connections'
    },
    
    {
        $project: {
           isValid: {$in: ['$connections.airline.name', '$validName']},
                         'connections.dst_airport': 1
       }
    },

    {
        $match: {isValid: true}
    }, 

    {
        $group: {
            _id: '$connections.dst_airport',
        }
    }  // 158 records returned.
])

My question is if we can use ‘restrictSearchWithMatch’ in $graphLookup() to filter the records instead of checking the validation [“Canadian Airlines”, “Iberia Airlines”, “Air Berlin”] after $graphLookup() stage? If I remove the comment in ‘restrictSearchWinMatch’ statement, the result is empty.

Hi @Qi.Chen,

I have 2 things to add here:

  1. Multiple choices can be true with slight modification. The question is designed in a way you need to find the best possible solution. Moreover, the answer options are basically shuffled so I cannot know which are choices B and C for you.
  2. The need for validation in a different stage is to ensure that we are applying that validation on the result of $graphLookup stage. The computations that we do in a stage need to have valid input.

Please feel free to reach out if you have any questions.

Kind Regards,
Sonali

Hi Sonali,
I really appreciate your input and hints. Thank you very much.
Qi

1 Like