0
0
MongoDBquery~10 mins

$addToSet for unique array additions in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $addToSet for unique array additions
Start Update
Check if value exists in array?
Skip add
Update document
Done
When updating a document, $addToSet checks if the value is already in the array. If not, it adds it uniquely.
Execution Sample
MongoDB
db.users.updateOne(
  { _id: 1 },
  { $addToSet: { hobbies: "reading" } }
)
This adds "reading" to the hobbies array only if it's not already there.
Execution Table
StepCurrent hobbies arrayValue to addExists in array?ActionResulting hobbies array
1["sports", "music"]"reading"NoAdd "reading"["sports", "music", "reading"]
2["sports", "music", "reading"]"reading"YesSkip add["sports", "music", "reading"]
3["sports", "music", "reading"]"music"YesSkip add["sports", "music", "reading"]
4["sports", "music", "reading"]"travel"NoAdd "travel"["sports", "music", "reading", "travel"]
5["sports", "music", "reading", "travel"]"sports"YesSkip add["sports", "music", "reading", "travel"]
6["sports", "music", "reading", "travel"]"cooking"NoAdd "cooking"["sports", "music", "reading", "travel", "cooking"]
7["sports", "music", "reading", "travel", "cooking"]"reading"YesSkip add["sports", "music", "reading", "travel", "cooking"]
ExitN/AN/AN/ANo more values to addFinal array unchanged
💡 All values processed; duplicates skipped; unique values added.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
hobbies array["sports", "music"]["sports", "music", "reading"]["sports", "music", "reading"]["sports", "music", "reading"]["sports", "music", "reading", "travel"]["sports", "music", "reading", "travel"]["sports", "music", "reading", "travel", "cooking"]["sports", "music", "reading", "travel", "cooking"]
Key Moments - 3 Insights
Why doesn't $addToSet add a value if it already exists in the array?
Because $addToSet ensures uniqueness by checking if the value is already present before adding. See execution_table rows 2 and 3 where "reading" and "music" are skipped since they exist.
What happens if the array field does not exist in the document?
MongoDB creates the array field and adds the value as the first element. This is not shown in the table but is important to know.
Can $addToSet add multiple values at once?
Yes, by using $each inside $addToSet. This example shows single values added one by one for clarity.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hobbies array after step 4?
A["sports", "music", "reading", "travel"]
B["sports", "music", "reading"]
C["sports", "music", "reading", "travel", "cooking"]
D["sports", "music"]
💡 Hint
Check the Resulting hobbies array column at step 4 in the execution_table.
At which step does the value "cooking" get added to the hobbies array?
AStep 3
BStep 6
CStep 2
DStep 5
💡 Hint
Look for the row where the Action is 'Add "cooking"' in the execution_table.
If we tried to add "music" again, what would happen according to the execution_table?
AIt would be added again, duplicating the value.
BThe array would be cleared first.
CIt would be skipped because it already exists.
DAn error would occur.
💡 Hint
See steps where the value exists and the action is 'Skip add' in the execution_table.
Concept Snapshot
$addToSet operator adds a value to an array only if it is not already present.
Syntax: { $addToSet: { field: value } }
Ensures array uniqueness without duplicates.
Can use $each to add multiple unique values.
If array field missing, it creates it with the value.
Useful for maintaining unique lists in documents.
Full Transcript
$addToSet is a MongoDB update operator that adds a value to an array only if that value does not already exist in the array. When you run an update with $addToSet, MongoDB checks the current array values. If the value is missing, it adds it. If the value is already present, it skips adding to avoid duplicates. This behavior helps keep arrays unique inside documents. The execution table shows step-by-step how values like "reading" and "travel" get added only once, while duplicates like "music" are skipped. If the array field does not exist, MongoDB creates it and adds the value. You can also add multiple unique values at once using $each inside $addToSet. This operator is useful for managing unique lists such as tags, hobbies, or categories in your MongoDB documents.