Introduction
Array Update Operators Recap
Adding a Single Item to Array
$push Modifiers
Adding Multiple Items to Array
Sort Items While Adding
Ensure Items Uniqueness

In many cases, you might want to ensure that an item is added to an array only if it does not already exist. This is where the $addToSet operator is beneficial.

use comic_book_store_db

db.comic_books.updateOne(
  { title: "The Amazing Spider-Man" },
  { $addToSet: { genres: "Science Fiction" }}
)

Here, we add a genre to the genres array but only if it is not already present. The $addToSet operator keeps your arrays free from duplicate values efficiently.

Removing Items from Array with $pull
Removing Specific Items from Array with $pullAll
Summary
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal