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

Implement $graphLookup.

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.3.8
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Backwards Compatibility:
      Fully Compatible
    • Epic Link:
    • Sprint:
      Query 13 (04/22/16), Query 14 (05/13/16)
    • Linked BF Score:
      0

      Description

      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”]
      }
      		]
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              david.storch David Storch
              Reporter:
              benjamin.murphy Benjamin Murphy
              Participants:
              Votes:
              1 Vote for this issue
              Watchers:
              14 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: