Challenge - 5 Problems
MongoDB $addToSet 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 aggregation using $addToSet?
Consider a collection
What will be the result of this aggregation?
orders with documents:{ "_id": 1, "items": ["apple", "banana"] }
{ "_id": 2, "items": ["banana", "orange"] }
{ "_id": 3, "items": ["apple", "orange"] }What will be the result of this aggregation?
db.orders.aggregate([
{ $unwind: "$items" },
{ $group: { _id: null, uniqueItems: { $addToSet: "$items" } } }
])
MongoDB
db.orders.aggregate([
{ $unwind: "$items" },
{ $group: { _id: null, uniqueItems: { $addToSet: "$items" } } }
])Attempts:
2 left
💡 Hint
Think about how $unwind breaks arrays into single elements and $addToSet collects unique values.
✗ Incorrect
The $unwind stage breaks each array element into its own document. Then $group with $addToSet collects all unique 'items' values into one array. Since all fruits appear across documents, the result is all unique fruits.
🧠 Conceptual
intermediate1:30remaining
What does $addToSet do in MongoDB aggregation?
Choose the best description of the $addToSet accumulator in MongoDB aggregation pipelines.
Attempts:
2 left
💡 Hint
Think about how $addToSet handles duplicates.
✗ Incorrect
$addToSet adds values to an array but only keeps unique values, removing duplicates automatically.
📝 Syntax
advanced2:30remaining
Which aggregation query correctly uses $addToSet to collect unique tags?
Given a collection
posts with documents containing a tags array, which aggregation pipeline correctly collects all unique tags across all posts?Attempts:
2 left
💡 Hint
Remember $addToSet collects unique values, but if you add arrays directly, it won't flatten them.
✗ Incorrect
Option B unwinds the tags array so each tag is a separate document, then $addToSet collects unique tags. Option B adds arrays as elements, not individual tags. Options C and D use $push which does not remove duplicates.
❓ optimization
advanced2:30remaining
How to optimize aggregation to collect unique values without $unwind?
You want to collect all unique tags from documents where each document has a
tags array. Which approach is more efficient and correct?Attempts:
2 left
💡 Hint
Think about how $addToSet treats arrays versus individual elements.
✗ Incorrect
Only by unwinding the array first can $addToSet collect unique individual tags. Using $addToSet on the array field adds whole arrays as elements, not individual tags.
🔧 Debug
expert3:00remaining
Why does this aggregation return nested arrays instead of unique values?
Given this aggregation:
Each
What is the cause?
db.products.aggregate([
{ $group: { _id: "$category", uniqueTags: { $addToSet: "$tags" } } }
])
Each
tags field is an array. The result shows uniqueTags as arrays inside an array, not a flat unique list.What is the cause?
Attempts:
2 left
💡 Hint
Think about what happens when you add arrays to a set.
✗ Incorrect
When $addToSet is used on an array field, it adds the whole array as a single element, resulting in nested arrays. To get unique individual elements, you must unwind first.