Introduction
Array Update Operators
Updating All Array Elements
Update Array Element by Index

Next, suppose you only want to update the first element of the characters array for the comic book titled "Guardians of the Galaxy". You can specify the index of the element directly.

use comic_book_store_db

db.comic_books.updateOne(
    { title: "Guardians of the Galaxy" },
    { $set: { "characters.0.has_abilities": true } }
)

Here, the dot notation characters.0.has_abilities is used to access the first element in the characters array (since arrays are 0-indexed). The updateOne method then sets the has_abilities field to true for the first character in "Guardians of the Galaxy"

Updating First Matching Array Element
Updating All Matching Array Elements
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