- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $graphLookup (aggregation)
$graphLookup (aggregation)¶
On this page
Changed in version 3.4.
Definition¶
-
$graphLookup
¶ Performs a recursive search on a collection, with options for restricting the search by recursion depth and query filter.
The
$graphLookup
search process is summarized below:Input documents flow into the
$graphLookup
stage of an aggregation operation.$graphLookup
targets the search to the collection designated by thefrom
parameter (see below for full list of search parameters).For each input document, the search begins with the value designated by
startWith
.$graphLookup
matches thestartWith
value against the field designated byconnectToField
in other documents in thefrom
collection.For each matching document,
$graphLookup
takes the value of theconnectFromField
and checks every document in thefrom
collection for a matchingconnectToField
value. For each match,$graphLookup
adds the matching document in thefrom
collection to an array field named by theas
parameter.This step continues recursively until no more matching documents are found, or until the operation reaches a recursion depth specified by the
maxDepth
parameter.$graphLookup
then appends the array field to the input document.$graphLookup
returns results after completing its search on all input documents.
$graphLookup
has the following prototype form:$graphLookup
takes a document with the following fields:Field Description from
Target collection for the $graphLookup
operation to search, recursively matching theconnectFromField
to theconnectToField
. Thefrom
collection cannot be sharded and must be in the same database as any other collections used in the operation. For information, see Sharded Collections.startWith
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.connectFromField
Field name whose value $graphLookup
uses to recursively match against theconnectToField
of other documents in the collection. If the value is an array, each element is individually followed through the traversal process.connectToField
Field name in other documents against which to match the value of the field specified by the connectFromField
parameter.as
Name of the array field added to each output document. Contains the documents traversed in the
$graphLookup
stage to reach the document.Note
Documents returned in the
as
field are not guaranteed to be in any order.maxDepth
Optional. Non-negative integral number specifying the maximum recursion depth. depthField
Optional. Name of the field to add to each traversed document in the search path. The value of this field is the recursion depth for the document, represented as a NumberLong
. Recursion depth value starts at zero, so the first lookup corresponds to zero depth.restrictSearchWithMatch
Optional. A document specifying additional conditions for the recursive search. The syntax is identical to query filter syntax.
Note
You cannot use any aggregation expression in this filter. For example, a query document such as
will not work in this context to find documents in which the
lastName
value is different from thelastName
value of the input document, because"$lastName"
will act as a string literal, not a field path.
Considerations¶
Sharded Collections¶
The collection specified in from
cannot be
sharded. However, the collection on which you run the
aggregate()
method can be sharded. That is, in
the following:
- The
collection
can be sharded. - The
fromCollection
cannot be sharded.
To join multiple sharded collections, consider:
- Modifying client applications to perform manual lookups instead of
using the
$graphLookup
aggregation stage. - If possible, using an embedded data model that removes the need to join collections.
Max Depth¶
Setting the maxDepth
field to 0
is equivalent to a
non-recursive $graphLookup
search stage.
Memory¶
The $graphLookup
stage must stay within the 100 megabyte
memory limit. If allowDiskUse: true
is specified for the
aggregate()
operation, the
$graphLookup
stage ignores the option. If there are other
stages in the aggregate()
operation,
allowDiskUse: true
option is in effect for these other stages.
See aggregration pipeline limitations for more information.
Views and Collation¶
If performing an aggregation that involves multiple views, such as
with $lookup
or $graphLookup
, the views must
have the same collation.
Examples¶
Within a Single Collection¶
A collection named employees
has the following documents:
The following $graphLookup
operation recursively matches
on the reportsTo
and name
fields in the employees
collection, returning the reporting hierarchy for each person:
The operation returns the following:
The following table provides a traversal path for the
document { "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
:
Start value | The |
---|---|
Depth 0 | |
Depth 1 | |
Depth 2 |
The output generates the hierarchy
Asya -> Ron -> Eliot -> Dev
.
Across Multiple Collections¶
Like $lookup
, $graphLookup
can access
another collection in the same database.
In the following example, a database contains two collections:
A collection
airports
with the following documents:A collection
travelers
with the following documents:
For each document in the travelers
collection, the following
aggregation operation looks up the nearestAirport
value in the
airports
collection and recursively matches the connects
field to the airport
field. The operation specifies a maximum
recursion depth of 2
.
The operation returns the following results:
The following table provides a traversal path for the recursive
search, up to depth 2
, where the starting airport
is JFK
:
Start value | The |
---|---|
Depth 0 | |
Depth 1 | |
Depth 2 |
With a Query Filter¶
The following example uses a collection with a set
of documents containing names of people along with arrays of their
friends and their hobbies. An aggregation operation finds one
particular person and traverses her network of connections to find
people who list golf
among their hobbies.
A collection named people
contains the following documents:
The following aggregation operation uses three stages:
$match
matches on documents with aname
field containing the string"Tanya Jordan"
. Returns one output document.$graphLookup
connects the output document’sfriends
field with thename
field of other documents in the collection to traverseTanya Jordan's
network of connections. This stage uses therestrictSearchWithMatch
parameter to find only documents in which thehobbies
array containsgolf
. Returns one output document.$project
shapes the output document. The names listed inconnections who play golf
are taken from thename
field of the documents listed in the input document’sgolfers
array.
The operation returns the following document: