- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Pipeline Aggregation 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. The
$lookupstage does an equality match between a field from the input documents with a field from the documents of the “joined” collection.To each input document, the
$lookupstage adds a new array field whose elements are the matching documents from the “joined” collection. The$lookupstage passes these reshaped documents to the next stage.The
$lookupstage has the following syntax:The
$lookuptakes a document with the following fields:Field Description fromSpecifies the collection in the same database to perform the join with. The fromcollection cannot be sharded. For information, see Sharded Collection Restrictions.localFieldSpecifies the field from the documents input to the $lookupstage.$lookupperforms an equality match on thelocalFieldto theforeignFieldfrom the documents of thefromcollection. If an input document does not contain thelocalField, the$lookuptreats the field as having a value ofnullfor matching purposes.foreignFieldSpecifies the field from the documents in the fromcollection.$lookupperforms an equality match on theforeignFieldto thelocalFieldfrom the input documents. If a document in thefromcollection does not contain theforeignField, the$lookuptreats the value asnullfor matching purposes.asSpecifies the name of the new array field to add to the input documents. The new array field contains the matching documents from the fromcollection. If the specified name already exists in the input document, the existing field is overwritten.
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.
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
collectioncan be sharded. - The
fromCollectioncannot 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
$lookupaggregation stage. - If possible, using an embedded data model that removes the need to join collections.
Examples¶
Perform a Join with $lookup¶
A collection orders contains the following documents:
Another collection inventory contains 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:
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: