$AddToSet MongoDB Operator
Overview
If a value is not already present in an array field, $addToSet MongoDB operator is used to add it. It makes sure that the array is not populated with duplicate values. By incorporating new elements into an existing array, this operator is frequently used to update documents.
$AddToSet MongoDB
If the value is already there, the $addToSet MongoDB operator does nothing to the array; otherwise, it adds the value to the array.
Syntax
Use dot notation for specifying a <field> in an embedded document or an array.
Behavior
The processing of document fields with string-based names is done via update operators in lexicographic order as of MongoDB 5.0. The processing of fields with numeric names is done in that sequence.
As opposed to affecting already-existing duplicate components, $addToSet MongoDB operator just makes sure that no duplicate items are added to the set. The ordering of the elements in the updated set is not guaranteed by the $addToSet function.
Starting with MongoDB 5.0, using an update operator like $addToSet MongoDB operator with an empty operand expression does not cause MongoDB to throw an error. No modifications are made as a result of an empty update, and no oplog record is written (indicating that the operation is a no-op).
How to Add Value in an Array?
You must mention the field and value in the update query in order to use $addToSet MongoDB operator to add a value to an array field.
Syntax for update query:
- The MongoDB database collection where the update is to be made is identified by the name db.collection.
- <query>: This is the filter or condition that designates which documents need to be updated. The syntax is identical to that of the find method.
- <update>: This option describes how to update the chosen documents. The update operators unset, addToSet, pull, and others can all be included in it. These operators specify how the matched documents' fields should be changed.
Here's an example:
The favoriteFriend array field of the document with the _id equal to "5948373189" receives the value "ritik" in the example above. "ritik" will be added to the array if it doesn't already contain it. If "ritik" already exists, nothing will change.
How to Add Duplicate Values in an Array?
The $addToSet MongoDB operator does not add duplicate values to an array by default. Use the $push operator instead if you wish to add duplicate values.
Syntax for $push:
Here's an example:
In this example, the document with the _id value of "5948373189" has the value "ritik" added to the favoriteFriend array field. Even if the value is already existing, it will still be added to the array.
How to use AddToSet?
Using $addToSet and the addToSet`. Here's an example:
In this example, the favoriteFriend array field of the document with the _id equal to "5948373189" is added using the $each operator and $addToSet. The values "naman", "bhavit", and "gautam" will be added to the array if any of them are not already there.
Working Demonstration
MongoDB Query:
Output:

FAQs
Q. Can I use $addToSet MongoDB operator to add several values to an array without using $each?
A. No, when using $addToSet to add several values to an array, you must use the each, $addToSet will add the entire array to the array field as a single value.
Q. What happens if I use $addToSet MongoDB operator on a field that is not an array?
A. MongoDB will generate an error if you use $addToSet on a field that isn't an array or doesn't exist. The $addToSet function was created specifically to work with array fields.
Conclusion
- In MongoDB, you can add values to an array field by using the $addToSet operator. It ensures that duplicate values are not added to the array.
- $addToSet creates a new array field and adds the supplied value to it if the field is not an array or doesn't exist.
- The $addToSet function does nothing and does not add a duplicate value if the field is an array and the given value is already present in the array.
- The value supplied by $addToSet is added to the array if the field is an array and it does not already contain the specified value.
- When using $addToSet and the $each operator, you can add multiple values to an array field.
- Instead of using $addToSet, you can use the $push operator to add duplicate values to an array.