Bird
Raised Fist0
MongoDBquery~20 mins

Why updating documents matters in MongoDB - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Document Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after updating a document?

Consider a MongoDB collection users with a document:

{ "name": "Alice", "age": 25 }

We run this update query:

db.users.updateOne({ "name": "Alice" }, { $set: { "age": 26 } })

What will be the value of age for Alice after this update?

MongoDB
db.users.updateOne({ "name": "Alice" }, { $set: { "age": 26 } })
db.users.find({ "name": "Alice" }).toArray()
A{"name": "Alice", "age": 25}
B{"name": "Alice"}
CNo document found
D{"name": "Alice", "age": 26}
Attempts:
2 left
💡 Hint

Think about what $set does in an update operation.

🧠 Conceptual
intermediate
1:30remaining
Why is updating documents important in databases?

Why do we need to update documents in a database like MongoDB?

ATo add new documents only
BTo change existing data without deleting and recreating documents
CTo delete all documents in a collection
DTo create indexes automatically
Attempts:
2 left
💡 Hint

Think about how data changes over time.

📝 Syntax
advanced
2:00remaining
Which update query correctly increments a field?

Given a document { "name": "Bob", "score": 10 }, which MongoDB update query correctly increases score by 5?

Adb.users.updateOne({ "name": "Bob" }, { $inc: { "score": 5 } })
Bdb.users.updateOne({ "name": "Bob" }, { $set: { "score": "score + 5" } })
Cdb.users.updateOne({ "name": "Bob" }, { $add: { "score": 5 } })
Ddb.users.updateOne({ "name": "Bob" }, { $increase: { "score": 5 } })
Attempts:
2 left
💡 Hint

Look for the operator that increments numeric fields.

🔧 Debug
advanced
2:00remaining
Why does this update query fail?

Consider this update query:

db.products.updateOne({ "id": 101 }, { "price": 19.99 })

Why will it replace the entire document instead of just updating the price field?

ABecause the update document is missing an update operator like $set
BBecause the query filter is incorrect
CBecause the collection name is wrong
DBecause the price value is not a string
Attempts:
2 left
💡 Hint

Check the structure of the update document.

optimization
expert
2:30remaining
How to efficiently update multiple documents?

You want to increase the stock field by 10 for all products with category "books". Which query is the most efficient?

Adb.products.updateOne({ "category": "books" }, { $inc: { "stock": 10 } })
Bdb.products.find({ "category": "books" }).forEach(doc => { db.products.updateOne({ _id: doc._id }, { $inc: { "stock": 10 } }) })
Cdb.products.updateMany({ "category": "books" }, { $inc: { "stock": 10 } })
Ddb.products.updateMany({}, { $inc: { "stock": 10 } })
Attempts:
2 left
💡 Hint

Consider the difference between updateOne and updateMany.

Practice

(1/5)
1. Why is it important to update documents in MongoDB instead of deleting and inserting new ones?
easy
A. Deleting and inserting is faster and safer.
B. Updating deletes the entire document automatically.
C. Updating keeps data consistent and avoids losing other fields.
D. MongoDB does not support updating documents.

Solution

  1. Step 1: Understand document update purpose

    Updating modifies only specific fields, keeping other data intact.
  2. Step 2: Compare update vs delete-insert

    Deleting and inserting risks losing data and is slower than updating.
  3. Final Answer:

    Updating keeps data consistent and avoids losing other fields. -> Option C
  4. Quick Check:

    Update preserves data = B [OK]
Hint: Update changes fields without losing data [OK]
Common Mistakes:
  • Thinking delete-insert is faster
  • Believing update removes whole document
  • Assuming MongoDB can't update documents
2. Which of the following is the correct syntax to update the field age to 30 in a MongoDB document?
easy
A. db.collection.updateOne({name: 'John'}, {$set: {age: 30}})
B. db.collection.updateOne({name: 'John'}, {age: 30})
C. db.collection.update({name: 'John'}, {$change: {age: 30}})
D. db.collection.updateOne({name: 'John'}, {$update: {age: 30}})

Solution

  1. Step 1: Identify correct update operator

    The $set operator updates specific fields without replacing the whole document.
  2. Step 2: Check syntax correctness

    Only db.collection.updateOne({name: 'John'}, {$set: {age: 30}}) uses updateOne with $set correctly.
  3. Final Answer:

    db.collection.updateOne({name: 'John'}, {$set: {age: 30}}) -> Option A
  4. Quick Check:

    Use $set to update fields = C [OK]
Hint: Use $set inside updateOne to change fields [OK]
Common Mistakes:
  • Omitting $set operator
  • Using wrong operator like $change or $update
  • Passing field directly without $set
3. Given the collection documents:
{"name": "Alice", "score": 50}

What will be the result after running:
db.collection.updateOne({name: "Alice"}, {$set: {score: 75}});
db.collection.find({name: "Alice"}).toArray();
medium
A. []
B. [{"name": "Alice", "score": 75}]
C. [{"name": "Alice"}]
D. [{"name": "Alice", "score": 50}]

Solution

  1. Step 1: Understand updateOne with $set

    The command changes the score field from 50 to 75 for the document where name is "Alice".
  2. Step 2: Check find query result

    The find query returns the updated document with score now 75.
  3. Final Answer:

    [{"name": "Alice", "score": 75}] -> Option B
  4. Quick Check:

    Update changes score to 75 = D [OK]
Hint: Update changes field value, find shows updated document [OK]
Common Mistakes:
  • Expecting old score after update
  • Thinking update removes other fields
  • Assuming update adds new document
4. What is wrong with this update command?
db.collection.updateOne({name: "Bob"}, {score: 100});
medium
A. Missing $set operator, so it replaces the whole document.
B. The filter query is incorrect syntax.
C. updateOne cannot update numeric fields.
D. The collection name is invalid.

Solution

  1. Step 1: Check update command structure

    The update document must use an operator like $set to update fields without replacing the whole document.
  2. Step 2: Understand effect of missing $set

    Without $set, the document is replaced entirely with {score: 100}, losing other fields.
  3. Final Answer:

    Missing $set operator, so it replaces the whole document. -> Option A
  4. Quick Check:

    Always use $set to update fields [OK]
Hint: Always include $set to update fields safely [OK]
Common Mistakes:
  • Omitting $set and replacing document
  • Thinking updateOne syntax is wrong
  • Believing updateOne can't update numbers
5. You want to update the status field to "active" only if it currently exists in the document. Which update command achieves this safely without creating new fields?
hard
A. db.collection.updateMany({status: {$exists: false}}, {$set: {status: "active"}})
B. db.collection.updateMany({}, {$set: {status: "active"}})
C. db.collection.updateMany({status: null}, {$set: {status: "active"}})
D. db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}})

Solution

  1. Step 1: Use filter to check field existence

    The filter {status: {$exists: true}} selects documents where status field exists.
  2. Step 2: Update only matching documents

    The $set updates status to "active" only for those documents, avoiding creating new fields.
  3. Final Answer:

    db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}}) -> Option D
  4. Quick Check:

    Filter with $exists true to update safely = A [OK]
Hint: Filter with $exists:true to update only existing fields [OK]
Common Mistakes:
  • Updating all documents regardless of field existence
  • Using $exists:false which matches missing fields
  • Filtering with null instead of $exists