Courseiva
CRUD Operations →hardMultiple Choice

C100DEV CRUD Operations Practice Question

A blog platform stores posts in the 'posts' collection. Each document has an array field 'comments' containing subdocuments with fields 'author' and 'text'. You need to add a new comment to the 'comments' array of a specific post identified by its '_id'. The new comment should be appended to the end of the array. Which update operation should you use?

⚠ Common exam trap

It's easy for candidates to confuse $push with $addToSet, which only adds if the element is not already present, but comments can be duplicated.

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

✓

db.posts.updateOne({ _id: postId }, { $push: { comments: { author: 'user1', text: 'Great post!' } } })

The $push operator is specifically designed to append values to an array. It adds the new comment subdocument to the 'comments' array without affecting existing elements. The other options either prevent duplicates, overwrite the array, or use an invalid operator. $push is the standard and correct choice for this scenario.

Answer analysis

Option-by-option breakdown

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

  • ✗

    db.posts.updateOne({ _id: postId }, { $set: { comments: { author: 'user1', text: 'Great post!' } } })

    Why it's wrong here

    $set replaces the entire 'comments' field with the new subdocument, turning the array into a single object. This would destroy all existing comments. It does not append; it overwrites. This is incorrect and would lead to data loss.

  • ✓

    db.posts.updateOne({ _id: postId }, { $push: { comments: { author: 'user1', text: 'Great post!' } } })

    Why this is correct

    The $push operator appends a specified value to an array. Here, it adds the new comment subdocument to the 'comments' array. This is the correct and efficient way to add an element to an array without needing to read and rewrite the entire document. It works atomically.

  • ✗

    db.posts.updateOne({ _id: postId }, { $addToSet: { comments: { author: 'user1', text: 'Great post!' } } })

    Why it's wrong here

    $addToSet adds an element to an array only if it does not already exist. If the same comment (exact match) is already present, it will not add it. The requirement is to add a new comment regardless of duplicates, as comments can be repeated. $addToSet would prevent duplicate comments with identical content, which is not desired.

  • ✗

    db.posts.updateOne({ _id: postId }, { $concat: { comments: { author: 'user1', text: 'Great post!' } } })

    Why it's wrong here

    $concat is not a valid update operator in MongoDB. It is an aggregation operator used for string concatenation in the aggregation pipeline. Using it in an update would cause an error. The correct operator for appending to an array is $push.

About these practice questions

Courseiva writes every C100DEV question from scratch — 259 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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 C100DEV 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 C100DEV exam.