0
0
MongoDBquery~20 mins

Why updating documents matters in MongoDB - Challenge Your Understanding

Choose your learning style9 modes available
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.