0
0
MongoDBquery~10 mins

$addToSet accumulator for unique 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 use $addToSet to add unique values to the array.

MongoDB
{ $group: { _id: "$category", items: { [1]: "$item" } } }
Drag options to blanks, or click blank then click option'
A$sum
B$push
C$addToSet
D$avg
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push instead of $addToSet, which allows duplicates.
Using $sum or $avg which are for numeric calculations, not arrays.
2fill in blank
medium

Complete the aggregation stage to group by "type" and collect unique "tags" using $addToSet.

MongoDB
{ $group: { _id: "$type", uniqueTags: { [1]: "$tags" } } }
Drag options to blanks, or click blank then click option'
A$push
B$max
C$first
D$addToSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push which does not remove duplicates.
Using $first or $max which return single values, not arrays.
3fill in blank
hard

Fix the error in the aggregation pipeline to correctly use $addToSet for unique "colors".

MongoDB
{ $group: { _id: "$brand", colors: { [1]: "$colors" } } }
Drag options to blanks, or click blank then click option'
A$addToSet
B$avg
C$sum
D$push
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push instead of $addToSet.
Using numeric accumulators like $sum or $avg for arrays.
4fill in blank
hard

Fill both blanks to group by "department" and collect unique "employees" using $addToSet.

MongoDB
{ $group: { _id: "$department", [1]: { [2]: "$employee" } } }
Drag options to blanks, or click blank then click option'
Astaff
B$push
C$addToSet
Dmembers
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push instead of $addToSet.
Using field names like "members" without matching the code.
5fill in blank
hard

Fill both blanks to group by "category", collect unique "products" with $addToSet, and name the array "uniqueProducts".

MongoDB
{ $group: { _id: "$category", [1]: { [2]: "$product" } } }
Drag options to blanks, or click blank then click option'
AuniqueProducts
B$addToSet
C$push
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using $push which allows duplicates.
Confusing field names or accumulator names.