Understanding the $lookup Operator
- The $lookup operator in MongoDB allows us to perform a left outer join between documents from two collections.
- This means we can perform the query of documents from one collection with data from another collection based on a specified matching condition.
- It’s useful for combining data from different collections for analysis or reporting purposes.
- The operator can handle cases where there are no matching documents in the foreign collection, providing flexibility in data retrieval.
Syntax:
The basic syntax of the $lookup operator is as follows:
{
$lookup: {
from: <foreignCollection>,
localField: <fieldInInputDocument>,
foreignField: <fieldInForeignDocument>,
as: <outputArrayField>
}
}
Explanation:
- from: The name of the foreign collection to join with.
- localField: The field from the input documents that will be used for matching.
- foreignField: The field from the foreign collection that will be used for matching.
- as: The name of the output array field that will contain the joined documents.
Let’s set up an Environment:
To understand MongoDB Aggregation $lookup we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called orders and customers which contains various information shown as below:
Collection: orders
[
{
"_id": ObjectId("60f9d7ac345b7c9df348a86e"),
"order_number": "ORD001",
"customer_id": ObjectId("60f9d7ac345b7c9df348a86d"),
"customer_details": [
{
"_id": ObjectId("60f9d7ac345b7c9df348a86d"),
"name": "John Doe",
"email": "john@example.com"
}
]
},
{
"_id": ObjectId("60f9d7ac345b7c9df348a86f"),
"order_number": "ORD002",
"customer_id": ObjectId("60f9d7ac345b7c9df348a86d"),
"customer_details": [
{
"_id": ObjectId("60f9d7ac345b7c9df348a86d"),
"name": "John Doe",
"email": "john@example.com"
}
]
},
{
"_id": ObjectId("60f9d7ac345b7c9df348a870"),
"order_number": "ORD003",
"customer_id": ObjectId("60f9d7ac345b7c9df348a86e"),
"customer_details": [
{
"_id": ObjectId("60f9d7ac345b7c9df348a86e"),
"name": "Alice Smith",
"email": "alice@example.com"
}
]
},
{
"_id": ObjectId("60f9d7ac345b7c9df348a871"),
"order_number": "ORD004",
"customer_id": ObjectId("60f9d7ac345b7c9df348a86f"),
"customer_details": [
{
"_id": ObjectId("60f9d7ac345b7c9df348a86f"),
"name": "Bob Johnson",
"email": "bob@example.com"
}
]
},
{
"_id": ObjectId("60f9d7ac345b7c9df348a872"),
"order_number": "ORD005",
"customer_id": ObjectId("60f9d7ac345b7c9df348a86f"),
"customer_details": [
{
"_id": ObjectId("60f9d7ac345b7c9df348a86f"),
"name": "Bob Johnson",
"email": "bob@example.com"
}
]
}
]
Collection: customers
[
{
"_id": ObjectId("60f9d7ac345b7c9df348a86d"),
"name": "John Doe",
"email": "john@example.com"
},
{
"_id": ObjectId("60f9d7ac345b7c9df348a86e"),
"name": "Alice Smith",
"email": "alice@example.com"
},
{
"_id": ObjectId("60f9d7ac345b7c9df348a86f"),
"name": "Bob Johnson",
"email": "bob@example.com"
}
]
MongoDB Aggregation $lookup
The $lookup operator in MongoDB is a powerful tool for performing join-like operations between documents from two collections. It allows us to perform join operations which are defined by documents from one collection with data from another collection based on a specified matching condition.
In this article, We will learn about MongoDB Aggregation $lookup along with multiple examples in detail.