M121: Lab 3 $lookup

Hi,

Could someone check ‘Lab - Using $lookup’?
I assume that the ‘Answer’ is not correct.
I always got: OneWorld 10.

If needed I can share my aggregate query.

Thanks,
Artem

Take a look at the data with Compass.

One mistake often made is to see the field as a string having only one airplane specified.

Another mistake that we have seen is to not see that sometimes more than one airplane is specified.

And the last mistake that occurred is to know that more than one airplane is specified but not realizing that it is not an array.

2 Likes

My query:
db.air_routes.aggregate([

{

    $match : {

         $or : [{ airplane : "747"}, { airplane : "380"}]

    }

},

{

    $lookup: {

      from: 'air_alliances',

      localField: 'airline.name',

      foreignField: 'airlines',

      as: 'air_alliance'

    }

},

{

    $match : {

        air_alliance : {

            $ne : []

        }

    }

},

{

    $group : {

        _id : "$air_alliance.name",

        sum : {$sum : 1}

    }

}

]).pretty()

One discrepancy here is that the solution uses airplane match expression /747|380/, that returns also routes where 747 or 380 are one of the possible airplanes that service the route. If you match routes that are serviced only by either 380 or 747, what you got back is the result of OneWorld : 10, SkyTeam: 8, etc.

I guess that the lab’s instructions could be formulated more clearly here.

Another thing that confused me a lot is the fact that KLM airlines is named just ‘KLM’ in air_alliances.airlines array, whereas it is named ‘KLM Royal Dutch Airlines’ in air_routes.airline.name field. Considering this, the ‘official’ result values are in fact not correct, as KLM belongs to SkyTeam alliance. The ‘winner’ would stay the same, but still…

This assignment can be definitely worked out a bit better.

3 Likes

Your query falls in the category: