A. Missing update operator like $set in the update document
B. Filter syntax is incorrect, $lt should be $gt
C. updateMany cannot update numeric fields
D. The collection name is invalid
Solution
Step 1: Check the update document
The update document {price: 90} lacks an update operator like $set. MongoDB requires operators to specify how to update fields.
Step 2: Validate filter and collection
The filter {price: {$lt: 100}} is correct, and collection name products is valid.
Final Answer:
Missing update operator like $set in the update document -> Option A
Quick Check:
Update needs $set or similar operator = A [OK]
Hint: Always use $set or $inc in update document [OK]
Common Mistakes:
Forgetting $set operator
Changing filter operator incorrectly
Assuming updateMany can't update numbers
5. You want to increase the stock by 5 for all products with category 'books' and set lastUpdated to the current date. Which updateMany command correctly does this in one operation?
hard
A. db.products.updateMany({category: 'books'}, {stock: {$inc: 5}, lastUpdated: new Date()})
B. db.products.updateMany({category: 'books'}, {$inc: {stock: 5}, $set: {lastUpdated: new Date()}})
C. db.products.updateMany({category: 'books'}, {$set: {stock: stock + 5, lastUpdated: new Date()}})
D. db.products.updateMany({category: 'books'}, {$inc: {stock: 5}, lastUpdated: new Date()})
Solution
Step 1: Use correct update operators together
To update multiple fields differently, combine $inc and $set inside one update document: {$inc: {...}, $set: {...}}.
Step 2: Validate syntax for date and increment
new Date() sets current date, and $inc: {stock: 5} increases stock by 5. This matches db.products.updateMany({category: 'books'}, {$inc: {stock: 5}, $set: {lastUpdated: new Date()}}).
Final Answer:
db.products.updateMany({category: 'books'}, {$inc: {stock: 5}, $set: {lastUpdated: new Date()}}) -> Option B
Quick Check:
Combine $inc and $set correctly = D [OK]
Hint: Combine $inc and $set in one update document [OK]