$lookup for joining collections
📖 Scenario: You work at a small online bookstore. You have two collections: books and authors. Each book has an author_id that matches the _id of an author. You want to combine information from both collections to see each book with its author's details.
🎯 Goal: Build a MongoDB aggregation query using $lookup to join the books collection with the authors collection, so each book document includes its author's information.
📋 What You'll Learn
Create a
books collection with three books, each having _id, title, and author_id fields.Create an
authors collection with three authors, each having _id and name fields.Write an aggregation query on
books using $lookup to join with authors on author_id and _id.Add a
$project stage to show only title and the joined author name.💡 Why This Matters
🌍 Real World
Joining collections is common when you have related data in different places, like customers and orders or products and suppliers.
💼 Career
Understanding $lookup is essential for MongoDB developers to combine data efficiently without duplicating it.
Progress0 / 4 steps