[SERVER-23725] Implement $graphLookup. Created: 14/Apr/16  Updated: 14/Mar/17  Resolved: 04/May/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.3.8

Type: Improvement Priority: Major - P3
Reporter: Benjamin Murphy Assignee: David Storch
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-23991 $graphLookup should optimize with $match Closed
is depended on by CSHARP-1649 Add support for $graphLookup aggregat... Closed
is depended on by JAVA-2187 Add builder for $graphLookup aggregat... Closed
is depended on by SERVER-23980 $graphLookup should spill to disk if ... Backlog
Documented
is documented by DOCS-7795 Document $graphLookup Closed
Duplicate
is duplicated by SERVER-25055 $graphLookup stage should implement g... Closed
Related
related to DRIVERS-297 Aggregation Framework Support for 3.4 Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 13 (04/22/16), Query 14 (05/13/16)
Participants:
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”]
}
		]
}



 Comments   
Comment by Githook User [ 04/May/16 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-23725 make agg_graph_lookup.js FSM workload take much less time
Branch: master
https://github.com/mongodb/mongo/commit/c1c1096d90bb335a503c87936230a9fa3e1f6a41

Comment by Githook User [ 04/May/16 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-23725 fix $graphLookup unit tests in pipeline_test
Branch: master
https://github.com/mongodb/mongo/commit/574e5f9f959abb5bd6891ac9dec38ad07e03f249

Comment by Githook User [ 04/May/16 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-23725 Aggregation now supports the graphLookup stage.

This commit is identical to a1253a94b82e65780d3aa3c4ddf92db02ec0b9d1.
Branch: master
https://github.com/mongodb/mongo/commit/34e784dd13375bba5547d332b6aeba9a80016e63

Comment by Githook User [ 30/Apr/16 ]

Author:

{u'username': u'visemet', u'name': u'Max Hirschhorn', u'email': u'max.hirschhorn@mongodb.com'}

Message: Revert "SERVER-23725 Aggregation now supports the graphLookup stage."

This reverts commit a1253a94b82e65780d3aa3c4ddf92db02ec0b9d1.
Branch: master
https://github.com/mongodb/mongo/commit/6c0f5951e3ed446d3e3107198a72f8a9975cf6e0

Comment by Benjamin Murphy [ 29/Apr/16 ]

This ticket added support for the $graphLookup stage, also known as recursive $lookup. It will need to be documented, and any drivers that provide aggregation helpers will need to provide support for it.

Comment by Githook User [ 29/Apr/16 ]

Author:

{u'username': u'benjaminmurphy', u'name': u'Benjamin Murphy', u'email': u'benjamin_murphy@me.com'}

Message: SERVER-23725 Aggregation now supports the graphLookup stage.
Branch: master
https://github.com/mongodb/mongo/commit/a1253a94b82e65780d3aa3c4ddf92db02ec0b9d1

Generated at Thu Feb 08 04:04:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.