Chapter 3: Lab - Using $lookup

I am not sure I understand the problem correctly.

Here is the problem:

Which alliance from air_alliances flies the most routes with either a Boeing 747 or an Airbus A380 (abbreviated 747 and 380 in air_routes)?

Here is how it came across to me:

If you take all routes, you filter it to only include “747” or “380”, which alliance flies the most.

My other theory of the question is:

Which alliance flies the most of its routes with one of the specified airplanes compared to all its routes?

I got it as “OneWorld” for both possibilities of the question. This is a wrong answer.

What am I missing?

Please do not help me with the answer, only with the question.

Thanks!

Just an idea:

As a first check, look at the fields, and see the type. I believe it is a string of airplane types say “Z X Y”.

Then a:"X" or a:"Y" won’t work as expected

Did I get the question right?

Hi @Elimelech_Wieder,

I can check that you have completed the lab successfully. You can refer to the “Detailed answer” section for better understanding.

If you still have any questions, please feel free to reach out.

Kind Regards,
Sonali

Hi Sonali, why do we have different results when we use: db.air_routes.aggregate([{$match: { “airplane”: { $in : [“747”, “380”] }}]) and when we use db.air_routes.aggregate([{$match: { “airplane”: /747|380/}}]) ?

I thought it was the same sentence wrote at different ways.
Thanks for clarify.

The $in operator works with array fields. It will match things like:

airplane : [ "747" , "320" ]
airplane : [ "333" , "380" , "901" ]

Note that $in works in some cases for string fields. The above $in will also match the following and only the following strings:

airplane : "747"
airplane : "380"

See https://docs.mongodb.com/manual/reference/operator/query/in/ for more info.

The other is a regular expression that works with strings. It will match things like:

airplane : "747 320"
airplane : "333 380 901"

See https://docs.mongodb.com/manual/reference/operator/query/regex/ for more info.

This being written, in a case like that, having an array would have been a much better schema design. But the curriculum team had to introduce regular expression in some way. This is as good as anything else.

Also

db.random.insert({a:["a", "b", "c"]})
db.random.find({a:/b/})
db.random.find({a:"b"})
db.random.find({a:/d|b/})

will find documents.

Though it seems this is because find traverses the arrays, more than a feature of regex.

1 Like