Courseiva
CRUD Operations →mediumMultiple Choice

C100DBA CRUD Operations Practice Question

Which operator is best used to add an element to an array only if it does not already exist?

⚠ Common exam trap

Candidates often suggest using $push in combination with application-level logic to check for duplicates, failing to recognize that $addToSet handles this requirement natively and atomically in the database.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

$addToSet

The $addToSet operator is the standard MongoDB tool for array manipulation when uniqueness is required. It ensures that the specified value is added to the array only if it is not currently present. This prevents duplicate entries without requiring the application to perform a read-then-write check, making it an atomic and highly performant operation for managing sets within document-based data structures.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    $push

    Why it's wrong here

    The $push operator adds a value to an array regardless of whether the value already exists in that array. This leads to duplicate entries, which may violate data integrity rules in applications that expect unique elements within a tag list or historical event log collection.

  • ✗

    $set

    Why it's wrong here

    The $set operator replaces the entire field value. Using $set to add an element would require overwriting the whole array, which is inefficient and risks overwriting concurrent changes made to the array by other application threads, leading to data inconsistency issues in high-concurrency environments.

  • ✓

    $addToSet

    Why this is correct

    This operator is specifically designed to treat an array as a mathematical set. It ensures uniqueness by verifying the element's absence before insertion. This atomic behavior is essential for maintaining clean data without the overhead of client-side validation logic or complex multi-step database interactions.

  • ✗

    $each

    Why it's wrong here

    The $each operator is a modifier used with $push or $addToSet to add multiple elements. It is not an operator that performs uniqueness checks by itself. It merely iterates over an array of values to be processed by the parent operator assigned to the update.

About these practice questions

One of 222 original C100DBA practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official MongoDB exam blueprint

This C100DBA practice question is part of Courseiva's free MongoDB certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the C100DBA exam.