$grapLookup: Cross collection lookup

Hello,

So in the code below, from the Cross Collection lookup video segment

 db.air_airlines.aggregate([
    {$match: {name: /TAP Portugal/ } },
    {$graphLookup: { 
        from: "air_routes",
        as: "chain",
        startWith: "$base",
        connectFromField: "dst_airport",
        connectToField: "src_airport",
        maxDepth: 0 }   // Set this to 0, not 1, so I could look at the first jump away from the base airport.
    }
]).pretty();

My understanding was that startWith, works with the connectFromField, which in the query above is dst_airport.

So the $base for TAP Portugal is OPO. Yet the data we get back from this query, is matching OPO to src_airport, what have I missed?

{
        "_id" : ObjectId("56e9b497732b6122f879157b"),
        "airline" : 4869,
        "name" : "TAP Portugal",
        "alias" : "TP",
        "iata" : "TAP",
        "icao" : "AIR PORTUGAL",
        "active" : "Y",
        "country" : "Portugal",
        "base" : "OPO",
        "chain" : [
                {
                        "_id" : ObjectId("56e9b39c732b6122f878edb7"),
                        "airline" : {
                                "id" : 2439,
                                "name" : "Formosa Airlines",
                                "alias" : "VY",
                                "iata" : "FOS"
                        },
                        "src_airport" : "OPO",
                        "dst_airport" : "BRU",
                        "codeshare" : "",
                        "stops" : 0,
                        "airplane" : "320"
                },
                {
                        "_id" : ObjectId("56e9b39c732b6122f878edb8"),
                        "airline" : {
                                "id" : 2439,
                                "name" : "Formosa Airlines",
                                "alias" : "VY",
                                "iata" : "FOS"
                        },
                        "src_airport" : "OPO",
                        "dst_airport" : "ORY",
                        "codeshare" : "",
                        "stops" : 0,
                        "airplane" : "320"
                },

Anyone got any ideas? It really bugs me, when I don’t “get” something.

From what I understand:

  1. We start from the collection air_airlines and the value of startWith: being the value of $base which is equal to OPO.
  2. We then do the first lookup from: air_routes, wanted the connectToField: to be src_airport to match the value we startWith:.
  3. For subsequents lookups, still from: air_routes, we find all documents with the value of dst_airport, the connectFromField:, to equals the value of src_airport, the connectToField:.

Basically the field startWith: is used in the original collection only as the value provider for the match. The connectFromField: is used in the from: collection and it provides the value for the match. The match happens between the connectToField: and the provided value.

1 Like

Ah the penny has just dropped.

The first comparision is

  1. OPO = src_airport

not

  1. OPO = dst_airport

Of into the garden to beat my head against a post. I am really worried about my mental competence … Why was I thinking option 2. is beyond me. I was reading the text completely the wrong way.

Thanks.