- 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:{ $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> } }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.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.
Examples¶
Perform a Join with $lookup¶
A collection orders contains the following documents:
{ "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2 }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1 }
{ "_id" : 3 }
Another collection inventory contains the following documents:
{ "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120 }
{ "_id" : 2, "sku" : "def", description: "product 2", "instock" : 80 }
{ "_id" : 3, "sku" : "ijk", description: "product 3", "instock" : 60 }
{ "_id" : 4, "sku" : "jkl", description: "product 4", "instock" : 70 }
{ "_id" : 5, "sku": null, description: "Incomplete" }
{ "_id" : 6 }
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:
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
])
The operation returns the following documents:
{
"_id" : 1,
"item" : "abc",
"price" : 12,
"quantity" : 2,
"inventory_docs" : [
{ "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120 }
]
}
{
"_id" : 2,
"item" : "jkl",
"price" : 20,
"quantity" : 1,
"inventory_docs" : [
{ "_id" : 4, "sku" : "jkl", "description" : "product 4", "instock" : 70 }
]
}
{
"_id" : 3,
"inventory_docs" : [
{ "_id" : 5, "sku" : null, "description" : "Incomplete" },
{ "_id" : 6 }
]
}
Use $lookup with an Array¶
If your localField is an array and you’d like to match the elements
inside it against a foreignField which is a single element, you’ll
need to $unwind the array as one stage of the aggregation
pipeline.
Consider a collection orders with the following document:
{ "_id" : 1, "item" : "MON1003", "price" : 350, "quantity" : 2, "specs" :
[ "27 inch", "Retina display", "1920x1080" ], "type" : "Monitor" }
Another collection inventory contains the following documents:
{ "_id" : 1, "sku" : "MON1003", "type" : "Monitor", "instock" : 120,
"size" : "27 inch", "resolution" : "1920x1080" }
{ "_id" : 2, "sku" : "MON1012", "type" : "Monitor", "instock" : 85,
"size" : "23 inch", "resolution" : "1280x800" }
{ "_id" : 3, "sku" : "MON1031", "type" : "Monitor", "instock" : 60,
"size" : "23 inch", "display_type" : "LED" }
The following aggregation operation performs a join on documents in the
orders collection which match a particular element of the specs
array to the size field in the inventory collection.
db.orders.aggregate([
{
$unwind: "$specs"
},
{
$lookup:
{
from: "inventory",
localField: "specs",
foreignField: "size",
as: "inventory_docs"
}
},
{
$match: { "inventory_docs": { $ne: [] } }
}
])
The operation returns the following document:
{
"_id" : 1,
"item" : "MON1003",
"price" : 350,
"quantity" : 2,
"specs" : "27 inch",
"type" : "Monitor",
"inventory_docs" : [
{
"_id" : 1,
"sku" : "MON1003",
"type" : "Monitor",
"instock" : 120,
"size" : "27 inch",
"resolution" : "1920x1080"
}
]
}