- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $lookup (aggregation)
$lookup (aggregation)¶
On this page
Definition¶
-
$lookup
¶ New in version 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. The$lookup
stage passes these reshaped documents to the next stage.
Syntax¶
The $lookup
stage has the following syntaxes:
Equality Match¶
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:
The $lookup
takes a document with the following fields:
Field | Description |
---|---|
from | Specifies the collection in the same database to perform
the join with. The |
localField | Specifies the field from the documents input to the
|
foreignField | Specifies the field from the documents in the |
as | Specifies the name of the new array field to add to the input
documents. The new array field contains the matching
documents from the |
The operation would correspond to the following pseudo-SQL statement:
See the following examples:
Consideration¶
Views and Collation¶
If performing an aggregation that involves multiple views, such as
with $lookup
or $graphLookup
, the views must
have the same collation.
Restrictions¶
Changed in version 4.2: You cannot include the
$out
or the$merge
stage in the$lookup
stage. That is, when specifying a pipeline for the joined collection, you cannot include either stage in thepipeline
field.
Sharded Collection Restrictions¶
In the $lookup
stage, the from
collection cannot be
sharded. However, the collection on which you run the
aggregate()
method can be sharded. That is, in
the following:
- The
collection
can be sharded. - The
fromCollection
cannot be sharded.
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:
- Modifying client applications to perform manual lookups instead of
using the
$lookup
aggregation stage. - If possible, using an embedded data model that removes the need to join collections.
Examples¶
Perform a Single Equality Join with $lookup
¶
Create a collection orders
with the following documents:
Create another collection inventory
with the following documents:
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:
The operation returns the following documents:
The operation would correspond to the following pseudo-SQL statement:
Use $lookup
with an Array¶
Starting MongoDB 3.4, if the localField
is an array, you can match
the array elements against a scalar foreignField
without needing an
$unwind
stage.
For example, create an example collection classes
with the
following document:
Create another collection members
with the following documents:
The following aggregation operation joins documents in the classes
collection with the members
collection, matching on the members
field to the name
field:
The operation returns the following:
Use $lookup
with $mergeObjects
¶
Changed in version 3.6: MongoDB 3.6 adds the $mergeObjects
operator to combine
multiple documents into a single document
Create a collection orders
with the following documents:
Create another collection items
with the following documents:
The following operation first uses the $lookup
stage to
join the two collections by the item
fields and then uses
$mergeObjects
in the $replaceRoot
to merge
the joined documents from items
and orders
:
The operation returns the following documents:
Specify Multiple Join Conditions with $lookup
¶
Changed in version 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.
Create a collection orders
with the following documents:
Create another collection warehouses
with the following documents:
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:
The operation returns the following documents:
The operation corresponds to the following pseudo-SQL statement:
The $expr
operator only uses indexes on the from
collection for equality matches. For example, if the index
{ stock_item: 1, instock: 1 }
exists on the warehouses
collection:
- The equality match on the
warehouses.stock_item
field uses the index.- The range part of the query on the
warehouses.instock
field does not use the indexed field in the compound index.
See also