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阶段。$graphLookupfrom 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:以下聚合操作使用三个阶段:
$matchname field containing the string "Tanya Jordan". name字段包含字符串"Tanya Jordan"的文档。$graphLookupfriends 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的文档。$projectconnections 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中使用图形数据