📖 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 array
Add a variable for the aggregation pipeline
Use $unwind on the authors field in the pipeline
Complete 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
1
Create the books collection with sample data
Create a variable called books and assign it an array with these exact documents: { title: 'Learn MongoDB', authors: ['Alice', 'Bob'] } and { title: 'Advanced MongoDB', authors: ['Carol'] }.
MongoDB
Hint
Use an array of objects with title and authors keys exactly as shown.
2
Create the aggregation pipeline variable
Create a variable called pipeline and assign it an empty array [] to start building the aggregation pipeline.
MongoDB
Hint
Start with an empty array for the pipeline.
3
Add the $unwind stage to the pipeline
Add an object with { $unwind: '$authors' } to the pipeline array to flatten the authors array.
MongoDB
Hint
Use { $unwind: '$authors' } inside the pipeline array.
4
Complete the aggregation query using the pipeline
Create a variable called result and assign it the aggregation of books using pipeline with db.collection.aggregate(pipeline) syntax. Assume db.collection is the collection holding books.
MongoDB
Hint
Use const result = db.books.aggregate(pipeline); to run the aggregation.
Practice
(1/5)
1. What does the $unwind stage do in a MongoDB aggregation pipeline?
easy
A. It turns each element of an array field into a separate document.
B. It groups documents by a specified field.
C. It sorts documents in ascending order.
D. It removes duplicate documents from the collection.
Solution
Step 1: Understand the purpose of $unwind
$unwind takes an array field and creates a new document for each element in that array.
Step 2: Compare with other pipeline stages
Grouping, sorting, and removing duplicates are done by other stages like $group, $sort, and $match, not $unwind.
Final Answer:
It turns each element of an array field into a separate document. -> Option A
Quick Check:
$unwind flattens arrays = B [OK]
Hint: Remember: $unwind splits arrays into separate documents [OK]
Common Mistakes:
Confusing $unwind with $group or $sort
Thinking $unwind removes duplicates
Assuming $unwind sorts documents
2. Which of the following is the correct syntax to use $unwind on the field items in an aggregation pipeline?
easy
A. { $unwind: "$items" }
B. { $unwind: { path: "$items" } }
C. { $unwind: items }
D. { $unwind: { path: "items" } }
Solution
Step 1: Recall correct $unwind syntax
The $unwind stage requires the field path as a string with a $ prefix inside an object with key path.
$unwind creates one document per array element, duplicating other fields.
Step 2: Apply $unwind to each document
Document 1 with tags ["red", "blue"] becomes two documents with tags "red" and "blue". Document 2 with tags ["green"] becomes one document with tag "green".
You want to create a list of all items ordered with their quantities as separate documents, but also keep documents that have no orders. Which aggregation pipeline correctly does this?