sunmingtao / sample-code

3 stars 4 forks source link

Mongo DB course M121 $graphLookup lab: What's the deal with both connectFromField and connectToField being "name" #75

Closed sunmingtao closed 4 years ago

sunmingtao commented 4 years ago

https://university.mongodb.com/mercury/M121/2019_November/chapter/Chapter_3_Core_Aggregation_-_Combining_Information/lesson/5980bfbd92e612ebe564aa4a/answer

The code in question is

$graphLookup: {
  startWith: "$airlines",
  from: "air_airlines",
  connectFromField: "name",
  connectToField: "name",
  as: "airlines",
  maxDepth: 0,
  restrictSearchWithMatch: {
    country: { $in: ["Germany", "Spain", "Canada"] }
  }
}

How come the value of both connectFromField and connectToField are name?

sunmingtao commented 4 years ago

At depth=0, the connection starts from startWith (air_alliances.airlines) to connectToField (air_airlines.name). Since the maxDepth is 0, the value of connectFromField shouldn't matter. You can even change it to name1, and it won't affect the result.

Assume maxDepth >0, at depth>0, the connection starts from connectFromField (air_airlines.name) to connectToField (air_airlines.name). When the values are the same, no connection is to be made.