0
0
MongoDBquery~20 mins

updateMany method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB updateMany Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this updateMany operation?

Consider a collection products with documents:

{ "_id": 1, "name": "Pen", "stock": 10 }
{ "_id": 2, "name": "Pencil", "stock": 5 }
{ "_id": 3, "name": "Eraser", "stock": 0 }

What will be the result of this command?

db.products.updateMany({ stock: { $lt: 10 } }, { $inc: { stock: 5 } })
MongoDB
db.products.updateMany({ stock: { $lt: 10 } }, { $inc: { stock: 5 } })
A{"acknowledged": true, "matchedCount": 1, "modifiedCount": 1}
B{"acknowledged": true, "matchedCount": 3, "modifiedCount": 3}
C{"acknowledged": true, "matchedCount": 2, "modifiedCount": 2}
D{"acknowledged": false, "matchedCount": 0, "modifiedCount": 0}
Attempts:
2 left
💡 Hint

Look for documents where stock is less than 10.

📝 Syntax
intermediate
2:00remaining
Which updateMany command is syntactically correct?

Choose the correct syntax to update all documents where status is "pending" by setting status to "completed".

Adb.orders.updateMany({ status == "pending" }, { $set: { status: "completed" } })
Bdb.orders.updateMany({ status: "pending" }, { $set: { status: "completed" } })
Cdb.orders.updateMany({ status: "pending" }, { status: "completed" })
Ddb.orders.updateMany({ status: "pending" }, { $update: { status: "completed" } })
Attempts:
2 left
💡 Hint

Remember the update document must use update operators like $set.

optimization
advanced
2:00remaining
How to optimize updateMany for large collections?

You want to update many documents in a large collection efficiently. Which approach improves performance?

AAdd an index on the fields used in the filter condition before running updateMany.
BRun updateMany without any indexes to avoid overhead.
CUse updateMany with an empty filter to update all documents at once.
DRun multiple updateOne commands instead of updateMany.
Attempts:
2 left
💡 Hint

Indexes help queries find documents faster.

🔧 Debug
advanced
2:00remaining
Why does this updateMany not modify any documents?

Given this command:

db.users.updateMany({ age: { $gt: 30 } }, { $set: { active: true } })

But no documents are modified even though some users have age 35 and 40. What is the likely cause?

MongoDB
db.users.updateMany({ age: { $gt: 30 } }, { $set: { active: true } })
AThe collection <code>users</code> does not exist.
BThe <code>$set</code> operator is missing in the update document.
CThe filter syntax is invalid and causes an error.
DThe <code>age</code> field is stored as a string, so the numeric filter fails.
Attempts:
2 left
💡 Hint

Check the data type of the age field in documents.

🧠 Conceptual
expert
2:00remaining
What does updateMany return after execution?

After running updateMany, what information does the returned object contain?

AAn object with <code>acknowledged</code>, <code>matchedCount</code>, and <code>modifiedCount</code> fields.
BThe number of documents in the collection.
CA boolean indicating if the update was successful.
DAn array of all updated documents.
Attempts:
2 left
💡 Hint

Think about what updateMany reports after running.