0
0
MongoDBquery~10 mins

$unwind for flattening arrays in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to unwind the 'items' array in the aggregation pipeline.

MongoDB
db.orders.aggregate([{ $[1]: "$items" }])
Drag options to blanks, or click blank then click option'
Amatch
Bunwind
Cgroup
Dproject
Attempts:
3 left
💡 Hint
Common Mistakes
Using $match instead of $unwind will filter documents but not flatten arrays.
Using $group or $project won't flatten arrays directly.
2fill in blank
medium

Complete the code to unwind the 'tags' array and preserve documents with empty or missing 'tags'.

MongoDB
db.products.aggregate([{ $unwind: { path: "$tags", [1]: true } }])
Drag options to blanks, or click blank then click option'
AallowDiskUse
BsortByCount
CpreserveNullAndEmptyArrays
DincludeArrayIndex
Attempts:
3 left
💡 Hint
Common Mistakes
Using includeArrayIndex adds the index but does not preserve empty arrays.
sortByCount and allowDiskUse are unrelated options.
3fill in blank
hard

Fix the error in the unwind stage to correctly specify the array field path.

MongoDB
db.sales.aggregate([{ $unwind: "[1]" }])
Drag options to blanks, or click blank then click option'
A$items
Bitems
C"items"
Ditems[]
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the $ sign causes the stage to fail.
Using brackets or quotes incorrectly changes the meaning.
4fill in blank
hard

Fill both blanks to unwind the 'comments' array and include the array index as 'commentIndex'.

MongoDB
db.posts.aggregate([{ $unwind: { path: "$comments", [1]: "[2]" } }])
Drag options to blanks, or click blank then click option'
AincludeArrayIndex
BpreserveNullAndEmptyArrays
CcommentIndex
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing preserveNullAndEmptyArrays with includeArrayIndex.
Using a wrong field name for the index.
5fill in blank
hard

Fill all three blanks to unwind the 'reviews' array, preserve empty arrays, and include the index as 'reviewPos'.

MongoDB
db.books.aggregate([{ $unwind: { path: "$reviews", [1]: true, [2]: "[3]" } }])
Drag options to blanks, or click blank then click option'
ApreserveNullAndEmptyArrays
BincludeArrayIndex
CreviewPos
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up option names or forgetting to quote the index field name.
Not preserving empty arrays when needed.