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 } })db.products.updateMany({ stock: { $lt: 10 } }, { $inc: { stock: 5 } })Look for documents where stock is less than 10.
The filter { stock: { $lt: 10 } } matches documents with stock less than 10, which are the Pencil and Eraser. Both get their stock increased by 5, so matchedCount and modifiedCount are 2.
Choose the correct syntax to update all documents where status is "pending" by setting status to "completed".
Remember the update document must use update operators like $set.
Option B uses the correct filter and update syntax with $set. Option B uses invalid filter syntax (==), Option B misses $set, and D uses a wrong update operator.
You want to update many documents in a large collection efficiently. Which approach improves performance?
Indexes help queries find documents faster.
Adding an index on the filter fields helps MongoDB quickly find matching documents, making updateMany faster. Running without indexes or updating all documents blindly is slower. Multiple updateOne calls add overhead.
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?
db.users.updateMany({ age: { $gt: 30 } }, { $set: { active: true } })Check the data type of the age field in documents.
If age is stored as a string, the numeric comparison $gt: 30 does not match any documents, so no updates occur.
After running updateMany, what information does the returned object contain?
Think about what updateMany reports after running.
updateMany returns an object showing if the operation was acknowledged, how many documents matched the filter, and how many were actually modified.