0
0
MongoDBquery~10 mins

$unwind for flattening arrays in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $unwind for flattening arrays
Start with document
Check array field
For each element in array
Create new document with element
Output each new document
Repeat for all documents
The $unwind stage takes each document with an array field and outputs a separate document for each element in that array, flattening the array.
Execution Sample
MongoDB
db.orders.aggregate([
  { $unwind: "$items" }
])
This pipeline takes each order document and outputs one document per item in the items array.
Execution Table
StepInput DocumentArray FieldUnwind ActionOutput Documents
1{"_id":1, "items":["apple", "banana", "cherry"]}["apple", "banana", "cherry"]Take each element in items array{"_id":1, "items":"apple"}, {"_id":1, "items":"banana"}, {"_id":1, "items":"cherry"}
2{"_id":2, "items":["orange"]}["orange"]Take each element in items array{"_id":2, "items":"orange"}
3{"_id":3, "items":[]}[]No elements to unwind, document removedNo output document
4{"_id":4, "items":null}nullField is null, document removedNo output document
5No more documents-End of input-
💡 All input documents processed; documents with empty or null arrays produce no output.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Current DocumentNone{"_id":1, "items":["apple", "banana", "cherry"]}{"_id":2, "items":["orange"]}{"_id":3, "items":[]}{"_id":4, "items":null}None
Array ElementsNone["apple", "banana", "cherry"]["orange"][]nullNone
Output Documents[][{"_id":1, "items":"apple"}, {"_id":1, "items":"banana"}, {"_id":1, "items":"cherry"}][{"_id":2, "items":"orange"}][][][]
Key Moments - 3 Insights
Why does a document with an empty array produce no output?
Because $unwind outputs one document per array element, if the array is empty, there are no elements to output, so the document is removed (see execution_table row 3).
What happens if the array field is null or missing?
If the field is null or missing, $unwind removes the document by default (see execution_table row 4).
How does $unwind handle multiple elements in the array?
It creates one output document per element, each with the same original fields but the array field replaced by the single element (see execution_table row 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output for the document with _id 2?
A{"_id":2, "items":["orange"]}
BNo output document
C{"_id":2, "items":"orange"}
DDocument removed
💡 Hint
Check execution_table row 2 under Output Documents
At which step does the condition of an empty array cause no output?
AStep 3
BStep 1
CStep 4
DStep 2
💡 Hint
Look at execution_table row 3 where Array Field is []
If the array field was missing instead of null in document _id 4, what would happen?
ADocument would be output with array field missing
BDocument would be removed, no output
CDocument would output one document with null
DError occurs
💡 Hint
Refer to key_moments about null or missing fields behavior
Concept Snapshot
$unwind stage syntax:
{ $unwind: "<arrayField>" }

Behavior:
- Outputs one document per element in the array field
- Removes documents with empty or null arrays by default
- Flattens arrays for easier processing in aggregation
Full Transcript
The $unwind stage in MongoDB aggregation takes each document with an array field and creates separate documents for each element in that array. For example, if a document has an items array with three elements, $unwind outputs three documents, each with one item. Documents with empty arrays or null array fields are removed by default. This helps flatten nested arrays for easier querying and processing. The execution table shows step-by-step how each input document is processed and what output documents are produced or removed.