0
0
MongoDBquery~5 mins

$addToSet accumulator for unique arrays in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $addToSet accumulator do in MongoDB aggregation?

$addToSet adds unique values to an array during aggregation. It prevents duplicates, so each value appears only once.

Click to reveal answer
beginner
How is $addToSet different from $push in MongoDB?

$push adds all values to an array, including duplicates. $addToSet adds only unique values, skipping duplicates.

Click to reveal answer
intermediate
Write a simple example of $addToSet usage in an aggregation pipeline.
{ $group: { _id: "$category", uniqueItems: { $addToSet: "$item" } } }

This groups documents by category and collects unique items in each group.

Click to reveal answer
intermediate
Can $addToSet be used to accumulate unique objects in an array?

Yes, $addToSet can accumulate unique objects. Uniqueness is based on the whole object content, not just a field.

Click to reveal answer
advanced
What happens if you use $addToSet on a field that contains arrays?

$addToSet treats the entire array as one value. It adds the array only if it is unique compared to others.

Click to reveal answer
What is the main purpose of $addToSet in MongoDB aggregation?
ATo add unique values to an array
BTo sum numeric values
CTo count documents
DTo sort documents
Which accumulator allows duplicates in the array?
A$avg
B$addToSet
C$sum
D$push
If you want to group documents by a field and collect unique values of another field, which accumulator do you use?
A$addToSet
B$sum
C$max
D$min
What will $addToSet do if the same object appears multiple times?
AAdd all copies
BAdd only one copy
CIgnore all copies
DThrow an error
Can $addToSet be used to accumulate unique arrays inside an array?
ANo, it flattens arrays automatically
BNo, it only works with numbers
CYes, it treats each array as a unique value
DYes, but only with strings
Explain how $addToSet helps in creating arrays without duplicates in MongoDB aggregation.
Think about collecting unique items when grouping data.
You got /3 concepts.
    Describe the difference between $addToSet and $push accumulators.
    One keeps duplicates, the other does not.
    You got /3 concepts.