$graphLookup: is $startsWith for $connectToField or $connectFromField?

In docs, it says that $startWith is the value that recursive search starts according to $connectFromField’s equality to it.

But in the video named [ $graphLookup: Cross Collection Lookup], if you set $maxDepth to 0 for the first query,

db.air_airlines.aggregate([ 
    { $match: {name: "TAP Portugal"} }, 
    { $graphLookup: {
        from: 'air_routes', 
        as: 'chain',
        startWith: '$base', 
        connectFromField: 'dst_airport', 
        connectToField: 'src_airport', 
        maxDepth: 0 
        }
    } 
]).pretty()

you see that result contains

	{
		"_id" : ObjectId("56e9b39c732b6122f878e40d"),
		"airline" : {
			"id" : 5265,
			"name" : "US Airways",
			"alias" : "US",
			"iata" : "USA"
		},
		"src_airport" : "OPO",
		"dst_airport" : "MAD",
		"codeshare" : "Y",
		"stops" : 0,
		"airplane" : "ER4"
	}

so from what I understand, first comparison is not $connectFromField = $startsWith but it is $connectToField = $startsWith.

Can you perhaps mention the exact time in the lecture this was mentioned? Saves time on having to re-watch the whole thing.

@Batuhan_Enes_63455,

So I had a bit more time to spare, I’ve re-watched the video and I can see where your confusion lies.

At a maxDepth of 0, $graphLookup does an initial equality match between startWith and connectToField. What this means is that, it is similar to a $lookup where localField == foreignField (in the case of $graphLookup, startWith == connectToField).
The recursion only happens at maxDepth > 0 between connectFromField and connectToField.

In conclusion, the documentation is accurate… the documentation (point 4) says startWith to connectToField, not startWith to connectFromField.

Thank you! I have just seen point 4 in the docs. But in the docs where every parameter is explained seperately, for startWith, it says

Expression that specifies the value of the connectFromField with which to start the recursive search. Optionally, startWith may be array of values, each of which is individually followed through the traversal process.

Is this not a contradiction to point 4 in the same doc?

I see! It’s just poorly worded. Because recursion doesn’t happen from startWith, what it’s trying to say is in two-fold:

  1. Expression that specifies the value that the connectToField is matched against to every document in the from collection
  2. hence, the value that the recursive search between connectFromField and connectToField is ultimately based upon

… I’m sure that it can also be worded differently. I’ll rope in the Curriculum Services Engineer, @Sonali_Mamgain.

2 Likes

Hi @007_jb @Batuhan_Enes_63455,

Thanks for sharing this feedback!!

We will take a deeper look into this and try to figure out the best way to resolve this issue.

Thanks,
Sonali

1 Like