Challenge - 5 Problems
MongoDB $unwind Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this $unwind operation?
Given the collection documents:
{ _id: 1, fruits: ["apple", "banana"] }What will be the result of this aggregation?[{ $unwind: "$fruits" }]Attempts:
2 left
💡 Hint
Think about how $unwind breaks down arrays into separate documents.
✗ Incorrect
The $unwind stage creates one document per element in the array, replacing the array field with the element value.
📝 Syntax
intermediate1:30remaining
Which $unwind syntax is correct to flatten the 'tags' array?
Choose the correct $unwind stage to flatten the 'tags' array in documents.
Attempts:
2 left
💡 Hint
Remember $unwind expects a string path with $ prefix or an object with path field.
✗ Incorrect
The correct syntax uses a string with $ prefix to specify the array field to unwind.
❓ query_result
advanced2:30remaining
What is the output when using $unwind with preserveNullAndEmptyArrays?
Given documents:
{ _id: 1, items: ["pen", "pencil"] }, { _id: 2 }What is the result of:[{ $unwind: { path: "$items", preserveNullAndEmptyArrays: true } }]Attempts:
2 left
💡 Hint
PreserveNullAndEmptyArrays keeps documents without the array or with empty arrays.
✗ Incorrect
Documents without the array field are included with the field set to null when preserveNullAndEmptyArrays is true.
❓ optimization
advanced2:00remaining
How to optimize $unwind when array field might be missing or empty?
You want to unwind the 'comments' array but keep documents without comments or with empty arrays. Which $unwind stage is best?
Attempts:
2 left
💡 Hint
Consider documents that may not have the array field or have it empty.
✗ Incorrect
Using preserveNullAndEmptyArrays: true keeps documents without the array or with empty arrays in the output.
🧠 Conceptual
expert2:30remaining
What happens if you $unwind a non-array field?
If a document has a field 'score' with a single integer value (not an array), what happens when you apply:
[{ $unwind: "$score" }]Attempts:
2 left
💡 Hint
Think about how $unwind treats fields that are not arrays.
✗ Incorrect
If the field is not an array, $unwind removes the document from the output unless preserveNullAndEmptyArrays is true.