$graphLookup and FieldPath field names may not start with a $

Good afternoon,

Q1. So, I had a little time so I thought I would ask, why in $graphLookup the connectFromField and ToField, are meant to be referenced without preceeding a $

$graphLookup: {
      from: 'child_reference',
      startWith: '$direct_reports',
      connectFromField: 'direct_reports',
      connectToField: 'name',
      as: 'upto_level_2_reports',
      maxDepth: 1,
     depthField: 'level'
}

Obviously my understanding is limited, but in other stages we reference pre existing fields with a $, apart from the match stage, unless using the $expr.

I just wondered what bit of logic I was missing, or is this one of those “Exceptions”?

Q2. So the output from the $graphLookup, via the as: seems a little random, so I assume that the normal process would be to $unwind the results, and use the depth field to put a bit of structure to the documents?

Is there a way to actually get the output in the such that children documents were within the parent document?

If you look at the documentation at

you will see that startWith accept an expression, so the need of $ to indicate that it is a field name.

Any reason for the the double star signs at the end of each lines in your $graphLookup?

2 Likes

@steevej-1495

I forgot to edit them out, when I was bolding/unbolding.

Thank you for that, since it just gave me a little light bulb moment, about when to use $fieldName and when not to, instead of it being a bit of a suck it and see strategy or copy a piece of code that works :wink:

At least I will have more of an idea, when to use $ and what expression really means.