Bird
Raised Fist0
MongoDBquery~20 mins

$unwind for flattening arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MongoDB $unwind Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2: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" }]
A[{ _id: 1, fruits: "apple" }, { _id: 1, fruits: "banana" }]
B[{ _id: 1, fruits: ["apple"] }, { _id: 1, fruits: ["banana"] }]
C[{ _id: 1, fruits: "apple" }]
D[{ _id: 1, fruits: ["apple", "banana"] }]
Attempts:
2 left
💡 Hint
Think about how $unwind breaks down arrays into separate documents.
📝 Syntax
intermediate
1:30remaining
Which $unwind syntax is correct to flatten the 'tags' array?
Choose the correct $unwind stage to flatten the 'tags' array in documents.
A{ $unwind: ["$tags"] }
B{ $unwind: "$tags" }
C{ $unwind: tags }
D{ $unwind: { path: "tags" } }
Attempts:
2 left
💡 Hint
Remember $unwind expects a string path with $ prefix or an object with path field.
❓ query_result
advanced
2: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 } }]
A[{ _id: 1, items: ["pen", "pencil"] }, { _id: 2 }]
B[{ _id: 1, items: "pen" }, { _id: 1, items: "pencil" }]
C[{ _id: 1, items: "pen" }, { _id: 1, items: "pencil" }, { _id: 2, items: null }]
D[{ _id: 1, items: "pen" }, { _id: 2, items: null }]
Attempts:
2 left
💡 Hint
PreserveNullAndEmptyArrays keeps documents without the array or with empty arrays.
❓ optimization
advanced
2: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?
A{ $unwind: { path: "$comments" } }
B{ $unwind: "$comments" }
C{ $unwind: { path: "$comments", preserveNullAndEmptyArrays: false } }
D{ $unwind: { path: "$comments", preserveNullAndEmptyArrays: true } }
Attempts:
2 left
💡 Hint
Consider documents that may not have the array field or have it empty.
🧠 Conceptual
expert
2: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" }]
AThe document is removed from the output because 'score' is not an array.
BThe aggregation throws an error because $unwind expects an array field.
CThe document is duplicated once for the single value of 'score'.
DThe document is passed through unchanged because 'score' is not an array.
Attempts:
2 left
💡 Hint
Think about how $unwind treats fields that are not arrays.

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

  1. Step 1: Understand the purpose of $unwind

    $unwind takes an array field and creates a new document for each element in that array.
  2. Step 2: Compare with other pipeline stages

    Grouping, sorting, and removing duplicates are done by other stages like $group, $sort, and $match, not $unwind.
  3. Final Answer:

    It turns each element of an array field into a separate document. -> Option A
  4. 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

  1. 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.
  2. Step 2: Evaluate each option

    { $unwind: "$items" } misses the path key. { $unwind: items } lacks quotes and $ prefix. { $unwind: { path: "items" } } misses the $ prefix. Only { $unwind: { path: "$items" } } correctly uses { path: "$items" }.
  3. Final Answer:

    { $unwind: { path: "$items" } } -> Option B
  4. Quick Check:

    Use { path: "$field" } for $unwind [OK]
Hint: Use { path: "$field" } syntax for $unwind [OK]
Common Mistakes:
  • Omitting quotes or $ in field path
  • Using string without path key
  • Passing field name without $ prefix
3. Given the collection documents:
{ "_id": 1, "tags": ["red", "blue"] }
{ "_id": 2, "tags": ["green"] }

What will be the output documents after applying this pipeline?
[ { $unwind: "$tags" } ]
medium
A. [{ "_id": 1, "tags": "blue" }, { "_id": 2, "tags": "green" }]
B. [{ "_id": 1, "tags": ["red", "blue"] }, { "_id": 2, "tags": ["green"] }]
C. [{ "_id": 1, "tags": "red" }, { "_id": 2, "tags": "green" }]
D. [{ "_id": 1, "tags": "red" }, { "_id": 1, "tags": "blue" }, { "_id": 2, "tags": "green" }]

Solution

  1. Step 1: Understand $unwind effect on arrays

    $unwind creates one document per array element, duplicating other fields.
  2. 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".
  3. Final Answer:

    [{ "_id": 1, "tags": "red" }, { "_id": 1, "tags": "blue" }, { "_id": 2, "tags": "green" }] -> Option D
  4. Quick Check:

    Each array element becomes a document = A [OK]
Hint: Each array item becomes a separate document after $unwind [OK]
Common Mistakes:
  • Expecting original array to remain intact
  • Missing duplicated documents for multiple array items
  • Confusing $unwind with $group
4. You run this aggregation pipeline:
[ { $unwind: "$comments" } ]

But some documents have no comments field or have it as an empty array. What problem occurs and how to fix it?
medium
A. Documents without comments are dropped; fix by adding preserveNullAndEmptyArrays: true in $unwind.
B. Syntax error occurs; fix by quoting the field name.
C. Documents are duplicated; fix by removing $unwind.
D. No problem occurs; $unwind handles missing fields automatically.

Solution

  1. Step 1: Identify behavior of $unwind on missing or empty arrays

    By default, $unwind removes documents where the array field is missing or empty.
  2. Step 2: Use preserveNullAndEmptyArrays option to keep those documents

    Adding preserveNullAndEmptyArrays: true keeps documents without the array or with empty arrays in the output.
  3. Final Answer:

    Documents without comments are dropped; fix by adding preserveNullAndEmptyArrays: true in $unwind. -> Option A
  4. Quick Check:

    Use preserveNullAndEmptyArrays to keep empty/missing arrays [OK]
Hint: Add preserveNullAndEmptyArrays:true to keep missing arrays [OK]
Common Mistakes:
  • Ignoring dropped documents without array
  • Assuming $unwind never drops documents
  • Confusing syntax errors with missing field issues
5. You have documents with a field orders which is an array of objects:
{ "_id": 1, "orders": [{ "item": "apple", "qty": 5 }, { "item": "banana", "qty": 3 }] }

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?
hard
A. [ { $unwind: { path: "$orders" } }, { $match: { orders: { $exists: true } } } ]
B. [ { $unwind: "$orders" }, { $project: { item: "$orders.item", qty: "$orders.qty" } } ]
C. [ { $unwind: { path: "$orders", preserveNullAndEmptyArrays: true } }, { $project: { item: "$orders.item", qty: "$orders.qty" } } ]
D. [ { $project: { item: "$orders.item", qty: "$orders.qty" } } ]

Solution

  1. Step 1: Use $unwind with preserveNullAndEmptyArrays to keep documents without orders

    [ { $unwind: { path: "$orders", preserveNullAndEmptyArrays: true } }, { $project: { item: "$orders.item", qty: "$orders.qty" } } ] uses preserveNullAndEmptyArrays: true to keep documents that have no orders.
  2. Step 2: Project only the needed fields from the unwound orders array

    The $project stage extracts item and qty from each order object.
  3. Final Answer:

    [ { $unwind: { path: "$orders", preserveNullAndEmptyArrays: true } }, { $project: { item: "$orders.item", qty: "$orders.qty" } } ] -> Option C
  4. Quick Check:

    Unwind with preserveNullAndEmptyArrays + project fields = D [OK]
Hint: Use preserveNullAndEmptyArrays:true to keep docs without array [OK]
Common Mistakes:
  • Omitting preserveNullAndEmptyArrays and losing documents
  • Projecting fields before unwind causing errors
  • Using $match to filter instead of preserveNullAndEmptyArrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard