Graphlookup connecttofield

Why does this query dosent return any values in bosses when I use “name” in connectToField instead of “_id”
{$graphLookup: { from:“parent_reference”, startWith:"$reports_to", connectFromField:“reports_to”, connectToField:“name”, as:“bosses”}} ]).pretty()

Can you provide the output of db.parent_reference.find( { “name” : “Shannon” } ).pretty()? And please provide the output within pre html tag.

“_id” : 9,
“name” : “Shannon”,
“title” : “VP Education”,
“reports_to” : 5

reports_to is 5, so you have a document where name is 5. Probably not, but you probably have with _id 5.

If you use name as your connectToField you must have a matching value from the connectFromField. If you want to connect with name, then reports_to should contain names not _ids.

1 Like


In addition to @steevej-1495, at the very beginning, startWith needs to match the connectToField. This is the very first operation before connectFromField and connectToField recursively finds matches. And like as already been explained, there will be no match between $reports_to (e.g. 5) and name (e.g. “Shannon”).


By the way, please make sure that you’re acknowledging all your threads. A simple “it’s resolved” will do. It’s a waste of our time when we respond to you and yet we get no feedback. Here are some examples of your unacknowledged threads: