One Query, Two Collections — How $lookup Makes MongoDB Smarter


Hey devs! 👋
Working with related data across collections in MongoDB? $lookup is your friend! 🔗
It lets you join documents from one collection with another—just like SQL joins. Ideal for combining users with orders, products with reviews, or students with grades. Let’s dive in! 🚀
When developers first shift from SQL to NoSQL databases like MongoDB, one of the biggest concerns is: "How do I perform joins without relational tables?"
Well, MongoDB has an answer — the $lookup
stage in its powerful Aggregation Framework.
In this article, we'll break down what $lookup
does, when and where it's used, and go through a real-world example so you can apply it confidently in your next project.
🔍 What is the $lookup
Stage in MongoDB?
The $lookup
stage in MongoDB's aggregation pipeline allows you to perform a left outer join between two collections. It enriches documents from one collection with related data from another.
Think of it like an SQL JOIN
— but for NoSQL.
Syntax:
{
$lookup: {
from: "<foreignCollection>",
localField: "<localField>",
foreignField: "<foreignField>",
as: "<outputField>"
}
}
Parameters:
from
: The name of the collection you want to join.localField
: The field in the current collection.foreignField
: The field in thefrom
collection to match.as
: The name of the new array field to add the joined data.
🌍 Real-World Scenario: E-commerce Platform
Let’s say you’re building an e-commerce platform. You have two collections:
orders
Collection:
{
"_id": 1,
"orderNumber": "ORD123",
"customerId": "CUST1001"
}
customers
Collection:
{
"_id": "CUST1001",
"name": "Alice Johnson",
"email": "alice@example.com"
}
Now, you want to show order details along with customer information. This is a classic use-case for $lookup
.
🛠️ Why Use $lookup
?
Avoid multiple queries: Combine data in a single query, reducing complexity.
Maintain normalized data: Keep related data in separate collections while still being able to retrieve them together.
Great for reporting: Perfect for analytics and dashboards that need merged data.
🧪 Real Example: Aggregation Pipeline with $lookup
Let’s write a simple pipeline to join orders
with customers
:
db.orders.aggregate([
{
$lookup: {
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customerDetails",
},
},
]);
Output:
{
"_id": 1,
"orderNumber": "ORD123",
"customerId": "CUST1001",
"customerDetails": [
{
"_id": "CUST1001",
"name": "Alice Johnson",
"email": "alice@example.com"
}
]
}
Now, each order document includes the matching customer data — neat and efficient!
🔧 Where is $lookup
Commonly Used?
E-commerce apps (orders + products/customers)
Social networks (users + posts/friends)
Educational platforms (students + courses/grades)
Admin dashboards (logs + user metadata)
Finance apps (transactions + accounts)
⚠️ Pro Tips & Best Practices
Index the
foreignField
to improve lookup performance.If you only need a single object from the joined collection, consider using
$unwind
to flatten the resulting array.Use
let
andpipeline
(advanced$lookup
) for more flexible matching conditions.
📦 Final Thoughts
MongoDB’s $lookup
stage is a powerful tool that brings relational-like features into a NoSQL world. It simplifies data retrieval across collections, making your backend code cleaner and more efficient.
If you're dealing with related data spread across multiple collections, $lookup is your go-to solution.
✨ Ready to level-up your MongoDB queries? Try using $lookup
in your next aggregation pipeline!
Thank You!
Thank you for reading!
I hope you enjoyed this post. If you did, please share it with your network and stay tuned for more insights on software development. I'd love to connect with you on LinkedIn or have you follow my journey on HashNode for regular updates.
Happy Coding!
Darshit Anjaria
Subscribe to my newsletter
Read articles from Darshit Anjaria directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Darshit Anjaria
Darshit Anjaria
I’m a problem solver at heart, driven by the idea of building solutions that genuinely make a difference in people’s everyday lives. I’m always curious, always learning, and always looking for ways to improve the world around me through thoughtful, impactful work. Beyond building, I love giving back to the community — whether it’s by sharing what I’ve learned through blogs, tutorials, or helpful insights. My goal is simple: to make technology a little more accessible and useful for everyone. Let’s learn, build, and grow together.