$unwind for flattening arrays in MongoDB
📖 Scenario: You work at an online bookstore. Each book document has a title and an array of authors. You want to list each author separately along with the book title.
🎯 Goal: Build a MongoDB aggregation pipeline that uses $unwind to flatten the authors array so each author appears in its own document with the book title.
📋 What You'll Learn
Create a collection called
books with documents containing title and authors arrayAdd a variable for the aggregation pipeline
Use
$unwind on the authors field in the pipelineComplete the aggregation query to return each author with the book title
💡 Why This Matters
🌍 Real World
Flattening arrays in documents is common when you want to analyze or display each item separately, such as listing each author of a book individually.
💼 Career
Database developers and data analysts often use $unwind in MongoDB to prepare data for reporting, filtering, or transforming nested array data.
Progress0 / 4 steps