Courseiva

C100DEV MongoDB Overview and Document Model Practice Question

A developer is building a MongoDB application that stores social media posts. Each post document includes an array of comments, and each comment is a subdocument with fields such as author, text, and timestamp. The developer later needs to update a specific comment's text. Which update operation should they use to modify only the targeted comment's text without replacing the entire comments array?

⚠ Common exam trap

The trap here is assuming that the positional $ operator works without a matching array condition in the query filter, which would cause an error or unintended update.

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, "comments.author": "Alice" }, { $set: { "comments.$.text": "updated text" } })

To update a specific subdocument within an array, MongoDB provides the positional $ operator, which acts as a placeholder for the first array element that matches the query condition. The query must include a filter on the array field to identify which element to update. This approach modifies only the targeted subdocument without affecting other elements or replacing the entire array.

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: "Alice", text: "updated text" }] } })

    Why it's wrong here

    This update replaces the entire comments array with a new array containing only one comment. All other comments on the post would be lost. The scenario requires modifying only a specific comment's text while preserving the rest of the array. Using a direct assignment to the comments field overwrites the whole array, which is destructive and not the intended operation.

  • ✓

    db.posts.updateOne({ _id: postId, "comments.author": "Alice" }, { $set: { "comments.$.text": "updated text" } })

    Why this is correct

    The query includes a condition that matches a specific element in the comments array (comments.author: "Alice"), and the update uses the positional $ operator to set the text field of the first matching array element. This is the correct way to update a single subdocument within an array without replacing the entire array. The $ operator refers to the matched array element, ensuring only that comment is modified.

  • ✗

    db.posts.updateOne({ _id: postId }, { $set: { "comments.$.text": "updated text" } })

    Why it's wrong here

    This uses the positional $ operator without an array filter in the query. The $ operator requires the query to include a condition that matches an element of the array. Without specifying which comment to match, MongoDB cannot determine the correct array index, so the operation fails or updates an unintended element. The query must include a filter on the comments array, such as { "comments.author": "Alice" }, for the positional operator to work correctly.

  • ✗

    db.posts.updateOne({ _id: postId }, { $push: { comments: { author: "Alice", text: "updated text" } } })

    Why it's wrong here

    $push adds a new comment subdocument to the comments array rather than modifying an existing one. This would create a duplicate or incorrect entry, not update the text of a specific comment. The scenario requires modifying an existing comment's text, not adding a new one. Using $push would result in an additional comment and fail to change the intended comment, leaving the original text unchanged.

About these practice questions

This C100DEV question is part of Courseiva's 259-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.