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.
Jump into concepts and practice - no test required
db.users.updateOne(
{ _id: 1 },
{ $addToSet: { hobbies: "reading" } }
)| Step | Current hobbies array | Value to add | Exists in array? | Action | Resulting hobbies array |
|---|---|---|---|---|---|
| 1 | ["sports", "music"] | "reading" | No | Add "reading" | ["sports", "music", "reading"] |
| 2 | ["sports", "music", "reading"] | "reading" | Yes | Skip add | ["sports", "music", "reading"] |
| 3 | ["sports", "music", "reading"] | "music" | Yes | Skip add | ["sports", "music", "reading"] |
| 4 | ["sports", "music", "reading"] | "travel" | No | Add "travel" | ["sports", "music", "reading", "travel"] |
| 5 | ["sports", "music", "reading", "travel"] | "sports" | Yes | Skip add | ["sports", "music", "reading", "travel"] |
| 6 | ["sports", "music", "reading", "travel"] | "cooking" | No | Add "cooking" | ["sports", "music", "reading", "travel", "cooking"] |
| 7 | ["sports", "music", "reading", "travel", "cooking"] | "reading" | Yes | Skip add | ["sports", "music", "reading", "travel", "cooking"] |
| Exit | N/A | N/A | N/A | No more values to add | Final array unchanged |
| Variable | Start | After 1 | After 2 | After 3 | After 4 | After 5 | After 6 | Final |
|---|---|---|---|---|---|---|---|---|
| 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"] |
$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.$addToSet operator in MongoDB?$addToSet$addToSet adds a value to an array only if that value is not already present, ensuring uniqueness.$addToSet only adds unique values without duplicates.$addToSet ensures unique additions [OK]tags using $addToSet?$addToSet, use $each inside it.$addToSet with $each to add multiple unique values. { $addToSet: { tags: ["mongodb", "database"] } } lacks $each, so it tries to add an array as a single element. Using $push inside or instead of $addToSet is incorrect as it doesn't ensure uniqueness.{ _id: 1, colors: ["red", "blue"] }, what will be the colors array after running this update?{ $addToSet: { colors: "blue" } }$addToSet will not add it again.tags array, but your update query:{ $addToSet: { tags: ["nodejs", "mongodb"] } } adds the entire array as one element instead of separate tags. What is the fix?$addToSet adds it as a single element, not multiple elements.$each inside $addToSet adds each element uniquely.{ _id: 1, items: ["apple", "banana"] } You want to add the items ["banana", "cherry", "apple", "date"] uniquely to the items array. Which update query will correctly add only the new unique items?$addToSet with $each adds multiple unique items. { $addToSet: { items: { $each: ["banana", "cherry", "apple", "date"] } } } correctly uses this syntax. { $addToSet: { items: ["banana", "cherry", "apple", "date"] } } adds the entire array as one element. { $push: { items: { $each: ["banana", "cherry", "apple", "date"] } } } uses $push which allows duplicates. { $set: { items: ["banana", "cherry", "apple", "date"] } } replaces the entire array, losing existing items.