LAB with $lookup and $unwind operator

For lab related with $lookup correct answer contains $unwind stage after lookup:
$lookup: {
from: “air_alliances”,
foreignField: “airlines”,
localField: “”,
as: “alliance”
$unwind: “$alliance”
Why $unwind stage is needed?
Without it, $lookup doesn’t work properly and filed ‘alliance’ is an empty array.

1 Like

Have you look at all the output documents?

1 Like

Ok… I’m having a real hard time with this lab…

I can get output… But my lookup is returning all empty arrays. This leads me to believe I’m all messed up LOL.

I am running my query from the air_alliances database.

My from is using the air_routes
local is airlines (that it the local field that contains the airline names)
foreign is the name field
I’m using an as of “Matched”

However, everything is coming back as empty… therefore I cannot match the plane type to use the count and get an answer… Recommendations?

The correct way to set this up is that I am using the FOREIGN field FROM the database I want to compare FROM (I.E the database that contains the foreignField operator) to the local field I want to compare to and output AS

This is right ???

Hi @David_Thompson,

I can check that you have successfully completed the Lab: $lookup. I think if you run the pipeline on air_routes collection and match the airplane field. Then, lookup from air_alliances collection. It should give you non-empty arrays.

If you were able to resolve this using any other method, feel free to share your thoughts.
Please let us know if you have any questions.

Kind Regards,
Sonali Mamgain

I do not remember having such an issue when I took the course but

For example, in air_routes, a KLM route with 747 shows as

  "name":"KLM Royal Dutch Airlines",
 "airplane":"330 777 74M 747"}

However in air_alliances, KLM part of SkyTeam is written as KLM so matching with will lead to an empy alliance match. We could use airline.iata but all others will not work because their name matches.

I was curious and found:

Thanks… I got the lab… One recommendation though… You really should look into teaching more about the $regex command… (your solution uses a regex command to match the airplanes used) Using a pattern match of /747|380/ matches all entries that have any combination of the airplanes used, but $regex is not even talked about in any course until this one and then it is only a hint.

Using $regex effectively reduces excessive stages and also simplifies the code. I really feel that helping students learn how to effectively use this is crucial to effective aggregation writing.

I also would like to point out that some of your course contradicts the Practical MongoDB Aggregations manual. I don’t remember if I got this manual link from the forum or from the course materials, but if it isn’t available in the course, I recommend putting it in the reference material for the course. It is really informative on how to write effective, optimized querries.

Thanks for listening.

1 Like