On this page本页内容
Changed in version 3.4.在版本3.4中更改。
$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:$graphLookup
搜索过程总结如下:
$graphLookup
stage of an aggregation operation.$graphLookup
阶段。$graphLookup
targets the search to the collection designated by the from
parameter (see below for full list of search parameters).$graphLookup
将搜索目标指向由from
参数指定的集合(有关搜索参数的完整列表,请参阅下文)。startWith
.startWith
指定的值开始。$graphLookup
matches the startWith
value against the field designated by connectToField
in other documents in the from
collection.$graphLookup
将startWith
值与from
集合中其他文档中connectToField
指定的字段相匹配。$graphLookup
takes the value of the connectFromField
and checks every document in the from
collection for a matching connectToField
value. $graphLookup
获取connectFromField
的值,并检查from
集合中的每个文档是否有匹配的connectToField
值。$graphLookup
adds the matching document in the from
collection to an array field named by the as
parameter.$graphLookup
将from
集合中的匹配文档添加到由as参数命名的数组字段中。
This step continues recursively until no more matching documents are found, or until the operation reaches a recursion depth specified by the 此步骤将递归继续,直到找不到更多匹配的文档,或者直到操作达到maxDepth
parameter. maxDepth
参数指定的递归深度。然后$graphLookup
then appends the array field to the input document. $graphLookup
将数组字段附加到输入文档。$graphLookup
returns results after completing its search on all input documents.$graphLookup
在完成对所有输入文档的搜索后返回结果。
$graphLookup
has the following prototype form:$graphLookup
具有以下原型形式:
$graphLookup
takes a document with the following fields:$graphLookup
获取包含以下字段的文档:
from |
$graphLookup operation to search, recursively matching the connectFromField to the connectToField . $graphLookup 操作的目标集合,递归地将connectFromField 与connectToField 匹配。from collection cannot be sharded and must be in the same database as any other collections used in the operation. from 集合不能被切分,并且必须与操作中使用的任何其他集合位于同一数据库中。 |
startWith |
connectFromField with which to start the recursive search. connectFromField 的值。startWith may be array of values, each of which is individually followed through the traversal process.startWith 可以是一个值数组,每个值在遍历过程中单独跟随。 |
connectFromField |
$graphLookup uses to recursively match against the connectToField of other documents in the collection. $graphLookup 用于递归匹配集合中其他文档的connectToField 。 |
connectToField |
connectFromField parameter.connectFromField 参数指定的字段值相匹配的其他文档中的字段名。 |
as |
Note
|
maxDepth |
|
depthField |
NumberLong . NumberLong 。 |
restrictSearchWithMatch |
Note
|
The collection specified in 在from
cannot be sharded. from
中指定的集合无法分片。However, the collection on which you run the 但是,可以对运行aggregate()
method can be sharded. aggregate()
方法的集合进行分片。That is, in the following:也就是说,在以下方面:
collection
can be sharded.collection
可以分片。fromCollection
cannot be sharded.fromCollection
无法分片。To join multiple sharded collections, consider:要加入多个分片集合,请考虑:
$graphLookup
aggregation stage.$graphLookup
聚合阶段。Setting the 将maxDepth
field to 0
is equivalent to a non-recursive $graphLookup
search stage.maxDepth
字段设置为0
相当于非递归的$graphLookup
搜索阶段。
The $graphLookup
stage must stay within the 100 megabyte memory limit. $graphLookup
阶段必须保持在100 MB内存限制内。If 如果为allowDiskUse: true
is specified for the aggregate()
operation, the $graphLookup
stage ignores the option. aggregate()
操作指定了allowDiskUse:true
,则$graphLookup
阶段将忽略该选项。If there are other stages in the 如果aggregate()
operation, allowDiskUse: true
option is in effect for these other stages.aggregate()
操作中还有其他阶段,allowDiskUse:true
选项对这些其他阶段有效。
See aggregration pipeline limitations for more information.有关更多信息,请参阅聚合管道限制。
If performing an aggregation that involves multiple views, such as with 如果执行涉及多个视图的聚合,例如使用$lookup
or $graphLookup
, the views must have the same collation.$lookup
或$graphLookup
,则这些视图必须具有相同的排序规则。
A collection named 名为employees
has the following documents:employees
的集合包含以下文档:
The following 以下$graphLookup
operation recursively matches on the reportsTo
and name
fields in the employees
collection, returning the reporting hierarchy for each person:$graphLookup
操作递归匹配employees
集合中的reportsTo
字段和name
字段,返回每个人的报告层次结构:
The operation returns the following:该操作返回以下内容:
The following table provides a traversal path for the document 下表提供了文档{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
:{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
的遍历路径:
| |
Depth 0 | |
Depth 1 | |
Depth 2 |
The output generates the hierarchy 输出生成层次结构Asya -> Ron -> Eliot -> Dev
.Asya -> Ron -> Eliot -> Dev
。
Like 与$lookup
, $graphLookup
can access another collection in the same database.$lookup
一样,$graphLookup
可以访问同一数据库中的另一个集合。
In the following example, a database contains two collections:在以下示例中,数据库包含两个集合:
airports
with the following documents:airports
具有下列文档:
travelers
with the following documents:travelers
具有以下文档:
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. travelers
集合中的每个文档,以下聚合操作将在airports
集合中查找nearestAirport
值,并递归地将connects
字段与airport
字段匹配。The operation specifies a maximum recursion depth of 该操作指定最大递归深度为2
.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
:2
,其中起始airport
为JFK
:
| |
Depth 0 | |
Depth 1 | |
Depth 2 |
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.golf
列为自己爱好的人。
A collection named 名为people
contains the following documents:people
的集合包含以下文档:
The following aggregation operation uses three stages:以下聚合操作使用三个阶段:
$match
name
field containing the string "Tanya Jordan"
. "Tanya Jordan"
的name
字段的文档。$graphLookup
connects the output document’s friends
field with the name
field of other documents in the collection to traverse Tanya Jordan's
network of connections. $graphLookup
将输出文档的friends
字段与集合中其他文档的name字段连接起来,以遍历Tanya Jordan
的连接网络。restrictSearchWithMatch
parameter to find only documents in which the hobbies
array contains golf
. restrictSearchWithMatch
参数仅查找hobbies
数组中包含golf
的文档。$project
shapes the output document. $project
将形成输出文档。connections who play golf
are taken from the name
field of the documents listed in the input document’s golfers
array.connections who play golf
中列出的名字取自输入文档的golfers
数组中列出的文档的name
字段。The operation returns the following document:该操作将返回以下文档:
Webinar: Working with Graph Data in MongoDB网络研讨会:在MongoDB中使用图形数据