On this page本页内容
$graphLookup
Changed in version 5.1.在版本5.1中更改。
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
from
parameter (see below for full list of search parameters).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
指定的字段相匹配。For each matching document, 对于每个匹配的文档,$graphLookup
takes the value of the connectFromField
and checks every document in the from
collection for a matching connectToField
value. $graphLookup
获取connectFromField
的值,并检查from
集合中的每个文档是否有匹配的connectToField
值。For each match, 对于每个匹配,$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
returns results after completing its search on all input documents.$graphLookup
将数组字段附加到输入文档$graphLookup
在完成对所有输入文档的搜索后返回结果。
$graphLookup
has the following prototype form:具有以下原型形式:
{ $graphLookup: { from: <collection>, startWith: <expression>, connectFromField: <string>, connectToField: <string>, as: <string>, maxDepth: <number>, depthField: <string>, restrictSearchWithMatch: <document> } }
$graphLookup
takes a document with the following fields:获取具有以下字段的文档:
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 |
|
maxDepth | |
depthField | NumberLong . NumberLong 。 |
restrictSearchWithMatch |
{ lastName: { $ne: "$lastName" } }
|
Starting in MongoDB 5.1, you can specify sharded collections in the 从MongoDB 5.1开始,您可以在from
parameter of $graphLookup
stages.$graphLookup
阶段的from
参数中指定分片集合。
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
的集合具有以下文档:
{ "_id" : 1, "name" : "Dev" } { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" } { "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" } { "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" } { "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" } { "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew" }
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
字段,返回每个人的报告层次结构:
db.employees.aggregate( [ { $graphLookup: { from: "employees", startWith: "$reportsTo", connectFromField: "reportsTo", connectToField: "name", as: "reportingHierarchy" } } ] )
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "name" : "Dev", "reportingHierarchy" : [ ] } { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev", "reportingHierarchy" : [ { "_id" : 1, "name" : "Dev" } ] } { "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot", "reportingHierarchy" : [ { "_id" : 1, "name" : "Dev" }, { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" } ] } { "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot", "reportingHierarchy" : [ { "_id" : 1, "name" : "Dev" }, { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" } ] } { "_id" : 5, "name" : "Asya", "reportsTo" : "Ron", "reportingHierarchy" : [ { "_id" : 1, "name" : "Dev" }, { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }, { "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" } ] } { "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew", "reportingHierarchy" : [ { "_id" : 1, "name" : "Dev" }, { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }, { "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" } ] }
The following table provides a traversal path for the document 下表提供了文档{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
:{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
的遍历路径:
Start value |
{ ... "reportsTo" : "Ron" } |
---|---|
Depth 0 | { "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" } |
Depth 1 | { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" } |
Depth 2 | { "_id" : 1, "name" : "Dev" } |
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:在以下示例中,数据库包含两个集合:
A collection 一个集合airports
with the following documents:airports
带有下列文档:
{ "_id" : 0, "airport" : "JFK", "connects" : [ "BOS", "ORD" ] } { "_id" : 1, "airport" : "BOS", "connects" : [ "JFK", "PWM" ] } { "_id" : 2, "airport" : "ORD", "connects" : [ "JFK" ] } { "_id" : 3, "airport" : "PWM", "connects" : [ "BOS", "LHR" ] } { "_id" : 4, "airport" : "LHR", "connects" : [ "PWM" ] }
A collection 一个集合travelers
with the following documents:travelers
带有下列文档:
{ "_id" : 1, "name" : "Dev", "nearestAirport" : "JFK" } { "_id" : 2, "name" : "Eliot", "nearestAirport" : "JFK" } { "_id" : 3, "name" : "Jeff", "nearestAirport" : "BOS" }
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
集合中的每个文档,以下聚合操作将在airport
集合中查找nearestAirport
值,并递归地将Connections
字段与airport
字段匹配。The operation specifies a maximum recursion depth of 该操作指定最大递归深度为2
.2
。
db.travelers.aggregate( [ { $graphLookup: { from: "airports", startWith: "$nearestAirport", connectFromField: "connects", connectToField: "airport", maxDepth: 2, depthField: "numConnections", as: "destinations" } } ] )
The operation returns the following results:该操作返回以下结果:
{ "_id" : 1, "name" : "Dev", "nearestAirport" : "JFK", "destinations" : [ { "_id" : 3, "airport" : "PWM", "connects" : [ "BOS", "LHR" ], "numConnections" : NumberLong(2) }, { "_id" : 2, "airport" : "ORD", "connects" : [ "JFK" ], "numConnections" : NumberLong(1) }, { "_id" : 1, "airport" : "BOS", "connects" : [ "JFK", "PWM" ], "numConnections" : NumberLong(1) }, { "_id" : 0, "airport" : "JFK", "connects" : [ "BOS", "ORD" ], "numConnections" : NumberLong(0) } ] } { "_id" : 2, "name" : "Eliot", "nearestAirport" : "JFK", "destinations" : [ { "_id" : 3, "airport" : "PWM", "connects" : [ "BOS", "LHR" ], "numConnections" : NumberLong(2) }, { "_id" : 2, "airport" : "ORD", "connects" : [ "JFK" ], "numConnections" : NumberLong(1) }, { "_id" : 1, "airport" : "BOS", "connects" : [ "JFK", "PWM" ], "numConnections" : NumberLong(1) }, { "_id" : 0, "airport" : "JFK", "connects" : [ "BOS", "ORD" ], "numConnections" : NumberLong(0) } ] } { "_id" : 3, "name" : "Jeff", "nearestAirport" : "BOS", "destinations" : [ { "_id" : 2, "airport" : "ORD", "connects" : [ "JFK" ], "numConnections" : NumberLong(2) }, { "_id" : 3, "airport" : "PWM", "connects" : [ "BOS", "LHR" ], "numConnections" : NumberLong(1) }, { "_id" : 4, "airport" : "LHR", "connects" : [ "PWM" ], "numConnections" : NumberLong(2) }, { "_id" : 0, "airport" : "JFK", "connects" : [ "BOS", "ORD" ], "numConnections" : NumberLong(1) }, { "_id" : 1, "airport" : "BOS", "connects" : [ "JFK", "PWM" ], "numConnections" : NumberLong(0) } ] }
The following table provides a traversal path for the recursive search, up to depth 下表提供了递归搜索的遍历路径,直至深度2
, where the starting airport
is JFK
:2
,其中起始机场为JFK
:
Start value |
{ ... "nearestAirport" : "JFK" } |
---|---|
Depth 0 | { "_id" : 0, "airport" : "JFK", "connects" : [ "BOS", "ORD" ] } |
Depth 1 | { "_id" : 1, "airport" : "BOS", "connects" : [ "JFK", "PWM" ] } { "_id" : 2, "airport" : "ORD", "connects" : [ "JFK" ] } |
Depth 2 | { "_id" : 3, "airport" : "PWM", "connects" : [ "BOS", "LHR" ] } |
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
的集合包含以下文档:
{ "_id" : 1, "name" : "Tanya Jordan", "friends" : [ "Shirley Soto", "Terry Hawkins", "Carole Hale" ], "hobbies" : [ "tennis", "unicycling", "golf" ] } { "_id" : 2, "name" : "Carole Hale", "friends" : [ "Joseph Dennis", "Tanya Jordan", "Terry Hawkins" ], "hobbies" : [ "archery", "golf", "woodworking" ] } { "_id" : 3, "name" : "Terry Hawkins", "friends" : [ "Tanya Jordan", "Carole Hale", "Angelo Ward" ], "hobbies" : [ "knitting", "frisbee" ] } { "_id" : 4, "name" : "Joseph Dennis", "friends" : [ "Angelo Ward", "Carole Hale" ], "hobbies" : [ "tennis", "golf", "topiary" ] } { "_id" : 5, "name" : "Angelo Ward", "friends" : [ "Terry Hawkins", "Shirley Soto", "Joseph Dennis" ], "hobbies" : [ "travel", "ceramics", "golf" ] } { "_id" : 6, "name" : "Shirley Soto", "friends" : [ "Angelo Ward", "Tanya Jordan", "Carole Hale" ], "hobbies" : [ "frisbee", "set theory" ] }
The following aggregation operation uses three stages:以下聚合操作使用三个阶段:
$match
name
field containing the string "Tanya Jordan"
. name
字段包含字符串"Tanya Jordan"
的文档。$graphLookup
friends
field with the name
field of other documents in the collection to traverse Tanya Jordan's
network of connections. friends
字段与集合中其他文档的name
字段连接,以遍历Tanya Jordan
的连接网络。restrictSearchWithMatch
parameter to find only documents in which the hobbies
array contains golf
. restrictSearchWithMatch
参数仅查找其中hobbies
数组包含golf
的文档。$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
字段。db.people.aggregate( [ { $match: { "name": "Tanya Jordan" } }, { $graphLookup: { from: "people", startWith: "$friends", connectFromField: "friends", connectToField: "name", as: "golfers", restrictSearchWithMatch: { "hobbies" : "golf" } } }, { $project: { "name": 1, "friends": 1, "connections who play golf": "$golfers.name" } } ] )
The operation returns the following document:操作将返回以下文档:
{ "_id" : 1, "name" : "Tanya Jordan", "friends" : [ "Shirley Soto", "Terry Hawkins", "Carole Hale" ], "connections who play golf" : [ "Joseph Dennis", "Tanya Jordan", "Angelo Ward", "Carole Hale" ] }
Webinar: Working with Graph Data in MongoDB网络研讨会:在MongoDB中使用图形数据