- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $lookup (aggregation)
$lookup (aggregation)¶
On this page
Definition¶
-
$lookup
¶ Changed in version 5.1.
Performs a left outer join to a 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.Starting in MongoDB 5.1,
$lookup
works across sharded collections.
Syntax¶
The $lookup
stage has the following syntaxes:
Equality Match with a Single Join Condition¶
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 this syntax:
The $lookup
takes a document with these fields:
Field | Description |
---|---|
from | Specifies the collection in the same database to perform the join with. Starting in MongoDB 5.1, the collection specified in 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 these examples:
Join Conditions and Subqueries on a Joined Collection¶
MongoDB 3.6 adds support for:
- Executing a pipeline on a joined collection.
- Multiple join conditions.
- Correlated and uncorrelated subqueries.
In MongoDB, a correlated subquery is a pipeline in a $lookup
stage that references
document fields from a joined collection. An uncorrelated subquery does
not reference joined fields.
Note
Starting in MongoDB 5.0, for an uncorrelated subquery in a
$lookup
pipeline stage containing a $sample
stage, the $sampleRate
operator, or the
$rand
operator, the subquery is always run again if
repeated. Previously, depending on the subquery output size, either the
subquery output was cached or the subquery was run again.
MongoDB correlated subqueries are comparable to SQL correlated subqueries, where the inner query references outer query values. An SQL uncorrelated subquery does not reference outer query values.
MongoDB 5.0 also supports concise correlated subqueries.
To perform correlated and uncorrelated subqueries with two collections,
and perform other join conditions besides a single equality match, use
this $lookup
syntax:
The $lookup
stage accepts a document with these fields:
Field | Description |
---|---|
from | Specifies the collection in the same database to perform the join operation. Starting in MongoDB 5.1, the |
let | Optional. Specifies variables to use in the pipeline stages. Use the variable expressions to
access the fields from the joined collection’s documents that are
input to the Note To reference variables in pipeline
stages, use the The let variables can be accessed by the
stages in the pipeline, including
additional
|
pipeline | Specifies the The The Note To reference variables in pipeline
stages, use the The let variables can be accessed by the
stages in the pipeline, including
additional
|
as | Specifies the name of the new array field to add to the joined documents. The new array field contains the matching documents from the joined collection. If the specified name already exists in the joined document, the existing field is overwritten. |
The operation corresponds to this pseudo-SQL statement:
See the following examples:
Behavior¶
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
the pipeline
field.
Atlas Search Support¶
Starting in v6.0, the pipeline
can contain the Atlas Search $search
stage as the first stage inside
the pipeline.
For example, when you Join Conditions and Subqueries on a Joined Collection or run
Correlated Subqueries Using Concise Syntax, you can specify
$search
inside the pipeline as shown below:
Sharded Collections¶
Starting in MongoDB 5.1, you can specify sharded collections
in the from
parameter of $lookup
stages.
|sbe-title|¶
Starting in version 6.0, MongoDB can use the slot-based execution
query engine to execute $lookup
stages
if all preceding stages in the pipeline can also be executed by the
|sbe-short| and none of the following conditions are true:
- The
$lookup
operation executes a pipeline on a joined collection. To see an example of this kind of operation, see Join Conditions and Subqueries on a Joined Collection. - The
$lookup
’slocalField
orforeignField
specify numeric components. For example:{ localField: "restaurant.0.review" }
. - The
from
field of any$lookup
in the pipeline specifies a view or sharded collection.
For more information, see $lookup Optimization.
Examples¶
Perform a Single Equality Join with $lookup
¶
Create a collection orders
with these documents:
Create another collection inventory
with these 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 these documents:
The operation corresponds to this pseudo-SQL statement:
Use $lookup
with an Array¶
If the localField
is an array, you can match the array elements
against a scalar foreignField
without an $unwind
stage.
For example, create an example collection classes
with these
documents:
Create another collection members
with these documents:
The following aggregation operation joins documents in the classes
collection with the members
collection, matching on the
enrollmentlist
field to the name
field:
The operation returns the following:
Use $lookup
with $mergeObjects
¶
The $mergeObjects
operator combines multiple documents
into a single document.
Create a collection orders
with these documents:
Create another collection items
with these 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 these documents: