Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-23725

Implement $graphLookup.

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.3.8
    • 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”]
      }
      		]
      }
      

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            benjamin.murphy Benjamin Murphy
            Votes:
            1 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated:
              Resolved: