On this page本页内容
$lookup¶New in version 3.2.版本3.2中的新功能。
Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. 对同一数据库中的未分片集合执行左外部联接,以从“联接”集合中筛选文档进行处理。To each input document, the $lookup stage adds a new array field whose elements are the matching documents from the “joined” collection. $lookup阶段向每个输入文档添加一个新的数组字段,其元素是来自“joined”集合的匹配文档。The $lookup stage passes these reshaped documents to the next stage.$lookup阶段将这些重新成形的文档传递到下一阶段。
The $lookup stage has the following syntaxes:$lookup阶段具有以下语法:
To perform an equality match between a field from the input documents with a field from the documents of the “joined” collection, the $lookup stage has the following syntax:要在输入文档中的字段与“联接”集合中的文档中的字段之间执行相等匹配,loopup阶段语法如下所示:
The $lookup takes a document with the following fields:$lookup获取具有以下字段的文档:
| from |
|
| localField |
|
| foreignField |
|
| as |
|
The operation would correspond to the following pseudo-SQL statement:该操作将对应于以下伪SQL语句:
See the following examples:请参见以下示例:
If performing an aggregation that involves multiple views, such as with $lookup or $graphLookup, the views must have the same collation.如果执行涉及多个视图的聚合,例如使用$lookup或$graphLookup,则这些视图必须具有相同的排序规则。
Changed in version 4.2.在版本4.2中更改。You cannot include the $out or the $merge stage in the $lookup stage. 在$lookup阶段中不能包含$out或$merge阶段。That is, when specifying a pipeline for the joined collection, you cannot include either stage in the 也就是说,在为联接的集合指定管道时,不能在pipeline field.pipeline字段中包含任何一个阶段。
In the $lookup stage, the 在from collection cannot be sharded. $lookup阶段中,无法分割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不能分片。As such, to join a sharded collection with an unsharded collection, you can run the aggregation on the sharded collection and lookup the unsharded collection; e.g.:因此,要将分片集合与非分片集合联接,可以在分片集合上运行聚合并查找非分片集合;例如。:
Alternatively, or to join multiple sharded collections, consider:或者,要加入多个分片集合,请考虑:
$lookup聚合阶段。$lookup$lookup执行单个相等联接¶Create a collection 使用以下文档创建orders with the following documents:orders集合:
Create another collection 使用以下文档创建另一个inventory with the following documents:inventory集合:
The following aggregation operation on the orders collection joins the documents from orders with the documents from the inventory collection using the fields item from the orders collection and the sku field from the inventory collection:orders集合上的以下聚合操作使用订单集合中的字段项和库存集合中的sku字段,将订单中的文档与库存集合中的文档关联起来:
The operation returns the following documents:该操作将返回以下文档:
The operation would correspond to the following pseudo-SQL statement:该操作将对应于以下伪SQL语句:
$lookup with an Array$lookup¶Starting MongoDB 3.4, if the 从MongoDB 3.4开始,如果localField is an array, you can match the array elements against a scalar foreignField without needing an $unwind stage.localField是一个数组,则可以将数组元素与标量foreignField进行匹配,而无需$unwind阶段。
For example, create an example collection 例如,使用以下文档创建示例集合classes with the following document:classes:
Create another collection 使用以下文档创建另一个集合members with the following documents:members:
The following aggregation operation joins documents in the 以下聚合操作将classes collection with the members collection, matching on the members field to the name field:classes集合中的文档与members集合联接,并在members字段与name字段上进行匹配:
The operation returns the following:该操作返回以下内容:
$lookup with $mergeObjects$lookup与$mergeObjects一起使用¶Changed in version 3.6:在版本3.6中更改:MongoDB 3.6 adds the $mergeObjects operator to combine multiple documents into a single documentMongoDB 3.6添加了$mergeObjects运算符以将多个文档合并到一个文档中
Create a collection 使用以下文档创建集合orders with the following documents:orders:
Create another collection 使用以下文档创建另一个集合items with the following documents:items:
The following operation first uses the $lookup stage to join the two collections by the 以下操作首先使用$lookup阶段通过item fields and then uses $mergeObjects in the $replaceRoot to merge the joined documents from items and orders:item字段连接两个集合,然后使用$replaceRoot中的$mergeObjects合并来自项和订单的连接文档:
The operation returns the following documents:该操作将返回以下文档:
$lookup$lookup指定多个联接条件¶Changed in version 3.6:在版本3.6中更改:MongoDB 3.6 adds support for executing a pipeline on the joined collection, which allows for specifying multiple join conditions as well as uncorrelated sub-queries.MongoDB 3.6增加了对在连接的集合上执行管道的支持,允许指定多个连接条件以及不相关的子查询。
Create a collection 使用以下文档创建集合orders with the following documents:orders:
Create another collection 使用以下文档创建另一个集合warehouses with the following documents:warehouses:
The following operation joins the 以下操作按物料和库存量是否足以覆盖订单数量将orders collection with the warehouse collection by the item and whether the quantity in stock is sufficient to cover the ordered quantity:orders集合与warehouse集合关联起来:
The operation returns the following documents:该操作将返回以下文档:
The operation corresponds to the following pseudo-SQL statement:该操作对应于以下伪SQL语句:
The $expr operator only uses indexes on the from collection for equality matches. $expr运算符仅使用from集合上的索引进行相等匹配。For example, if the index 例如,如果{ stock_item: 1, instock: 1 } exists on the warehouses collection:warehouses集合上存在索引{ stock_item: 1, instock: 1 }:
The equality match on thewarehouses.stock_itemfield uses the index.warehouses.stock_item字段上的相等匹配使用索引。The range part of the query on thewarehouses.instockfield does not use the indexed field in the compound index.warehouses.instock字段查询的范围部分不使用复合索引中的索引字段。
See also参阅