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:以下聚合操作使用三个阶段:
$matchname 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中使用图形数据