-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
None
-
Fully Compatible
-
Query 13 (04/22/16), Query 14 (05/13/16)
-
0
Behavioral Description
A $graphLookup stage in a pipeline will have the following form:
{$graphLookup: from: <name of collection to look up into>, startWith: <expression>, connectFromField: <name of field in document from “from”>, connectToField: <name of field in document from “from”>, as: <name of field in output document>, maxDepth: <optional - non-negative integer>, depthField: <optional - name of field in output documents> }
Here’s the meaning of each field:
- from: Equivalent to the from field in $lookup, this specifies the collection that this stage retrieves results from.
- startWith: Specifies the connectToField value(s) that we should start our recursive search with. For example, this could be a list of Twitter handles.
- connectFromField: Specifies a field in each document in the from collection that is used to perform the next recursive query. For example, this could be a list of Twitter handles of users that have followed the New York Times.
- connectToField: Specifies a field in each document in the from collection that is queried against with each recursive query. For example, this could be a user’s Twitter handle.
- as: Specifies a field in the output document that will be filled with the array of results of the $graphLookup.
- maxDepth: (optional) Specifies a maximum recursive depth for the $graphLookup.
- depthField: (optional) Specifies a field in each result document that will be set to the recursive depth at which the document was retrieved. For example, this value might represent the number of flights you need to take to get from New York to Beijing.
Examples
Given a starting person, compute their friend network.
Input:
{ _id: 0, name: “Bob Smith”, friends: [“Anna Jones”, “Chris Green”] }
Contents of from Collection, contacts:
{ _id: 1, name: “Anna Jones”, friends: [“Bob Smith”, “Chris Green”, “Joe Lee”] }, { _id: 0, name: “Bob Smith”, friends: [“Anna Jones”, “Chris Green”] }, { _id: 2, name: “Chris Green”, friends: [“Anna Jones”, “Bob Smith”] }, { _id: 3, name: “Joe Lee”, friends: [“Anna Jones”, “Fred Brown”] }, { _id: 4, name: “Fred Brown”, friends: [“Joe Lee”] }
Pipeline:
{ $graphLookup: { from: “contacts”, startWith: “$friends”, connectFromField: “friends”, connectToField: “name”, as: “socialNetwork” } }
Output:
{ name: “Bob Smith”, friends: [“Anna Jones”, “Chris Green”], socialNetwork: [ { _id: 1, name: “Anna Jones”, friends: [“Bob Smith”, “Chris Green”, “Joe Lee”] }, { _id: 0, name: “Bob Smith”, friends: [“Anna Jones”, “Chris Green”] } { _id: 2, name: “Chris Green”, friends: [“Anna Jones”, “Bob Smith”] }, { _id: 3, name: “Joe Lee”, friends: [“Anna Jones”, “Fred Brown”] }, { _id: 4, name: “Fred Brown”, friends: [“Joe Lee”] } ] }
- is depended on by
-
SERVER-23991 $graphLookup should optimize with $match
- Closed
-
CSHARP-1649 Add support for $graphLookup aggregation stage
- Closed
-
JAVA-2187 Add builder for $graphLookup aggregation stage
- Closed
-
SERVER-23980 $graphLookup should spill to disk if allowDiskUse is specified
- In Progress
- is duplicated by
-
SERVER-25055 $graphLookup stage should implement getDependencies()
- Closed
- related to
-
DRIVERS-297 Aggregation Framework Support for 3.4
- Closed