0
0
MongoDBquery~20 mins

$addToSet accumulator for unique arrays in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $addToSet 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 aggregation using $addToSet?
Consider a collection 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" } } }
])
A{"_id": null, "uniqueItems": ["apple", "banana", "orange"]}
B{"_id": null, "uniqueItems": ["apple", "banana"]}
C{"_id": null, "uniqueItems": ["banana", "orange"]}
D{"_id": null, "uniqueItems": ["apple", "orange"]}
Attempts:
2 left
💡 Hint
Think about how $unwind breaks arrays into single elements and $addToSet collects unique values.
🧠 Conceptual
intermediate
1:30remaining
What does $addToSet do in MongoDB aggregation?
Choose the best description of the $addToSet accumulator in MongoDB aggregation pipelines.
ACounts the number of documents in a group.
BAdds all values including duplicates to an array.
CAdds only unique values to an array, ignoring duplicates.
DCalculates the sum of numeric values.
Attempts:
2 left
💡 Hint
Think about how $addToSet handles duplicates.
📝 Syntax
advanced
2: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?
Adb.posts.aggregate([{ $unwind: "$tags" }, { $group: { _id: null, allTags: { $push: "$tags" } } }])
Bdb.posts.aggregate([{ $unwind: "$tags" }, { $group: { _id: null, allTags: { $addToSet: "$tags" } } }])
Cdb.posts.aggregate([{ $group: { _id: null, allTags: { $push: "$tags" } } }])
Ddb.posts.aggregate([{ $group: { _id: null, allTags: { $addToSet: "$tags" } } }])
Attempts:
2 left
💡 Hint
Remember $addToSet collects unique values, but if you add arrays directly, it won't flatten them.
optimization
advanced
2: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?
AUse $unwind on tags, then $group with $addToSet on tags.
BUse $project to flatten tags, then $group with $push.
CUse $group with $addToSet directly on the tags array field.
DUse $group with $push on tags, then $unwind the result.
Attempts:
2 left
💡 Hint
Think about how $addToSet treats arrays versus individual elements.
🔧 Debug
expert
3:00remaining
Why does this aggregation return nested arrays instead of unique values?
Given this aggregation:
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?
AMongoDB does not support $addToSet on arrays.
BThe _id field should be null to collect all tags.
CThe $group stage is missing a $unwind before grouping.
D$addToSet adds the entire tags array as one element, causing nested arrays.
Attempts:
2 left
💡 Hint
Think about what happens when you add arrays to a set.