Chapter 3 - Lab $lookup - What's wrong with pipeline?

I am using the following aggregation pipeline which is matching to what is mentioned in the answer:

var pipeline1 = [
{ $match: { airplane: /747|380/ } },
{ $lookup: { from: “air_alliances”, localField: “airlines”, foreignField: “airline.name”, as: “alliance”} },
{ $unwind: “$alliance” },
{ $group: { _id: “$alliance.name”, count: { “$sum”: 1 } } },
{ $sort: { count: -1 } }
]

But executing this pipeline is giving me wrong result.

Can someone let me know what’s wrong with this pipeline?

Help us help you by telling us more about what is wrong with the result you get. Wrong order? Wrong list of documents?

Are you sure about:

See $lookup (aggregation) — MongoDB Manual for the syntax and the significance of localField and foreignField. There is no field named airlines in the input documents from air_routes.

I am executing this pipeline against air_routes. The localField “airlines” is from dataset air_alliance. The pipeline produced the below result:

{ “_id” : “SkyTeam”, “count” : 89}
{ “_id” : “OneWorld”, “count” : 89 }
{ “_id” : “Star Alliance”, “count” : 89 }

That’s correct. So your input documents are from air_routes.

The above is not correct. That is why I mentioned

and

This is also why I mentioned

From the detailed answer of the lab they have:

    foreignField: "airlines",
    localField: "airline.name",

but you have

    localField: "airlines",
    foreignField: "airline.name"
1 Like

Hey @Naveen_Mundhra,

I see that you already completed the Lab: Using $lookup :clap:

However, if you still have any additional questions, please feel free to reach out.

Kind Regards,
Sonali