Bird
Raised Fist0
MongoDBquery~20 mins

updateMany method in MongoDB - Practice Problems & Coding Challenges

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
🎖️
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.

Practice

(1/5)
1. What does the updateMany method do in MongoDB?
easy
A. Inserts multiple new documents
B. Deletes multiple documents at once
C. Finds a single document by ID
D. Updates multiple documents that match a filter

Solution

  1. Step 1: Understand the purpose of updateMany

    The updateMany method is designed to update all documents that match a given filter in a collection.
  2. Step 2: Compare with other operations

    Deleting documents is done by deleteMany, inserting by insertMany, and finding by findOne. So, only updateMany updates multiple documents.
  3. Final Answer:

    Updates multiple documents that match a filter -> Option D
  4. Quick Check:

    updateMany updates multiple documents = C [OK]
Hint: updateMany changes many documents matching filter [OK]
Common Mistakes:
  • Confusing updateMany with deleteMany
  • Thinking updateMany inserts documents
  • Believing updateMany finds documents
2. Which of the following is the correct syntax to update the field status to active for all documents where age is greater than 30 using updateMany?
easy
A. db.collection.updateMany({age: {$gt: 30}}, {$set: {status: 'active'}})
B. db.collection.updateMany({age: > 30}, {status: 'active'})
C. db.collection.updateMany({age: {$gt: 30}}, {status: 'active'})
D. db.collection.updateMany({age: {$gt: 30}}, {$update: {status: 'active'}})

Solution

  1. Step 1: Check filter syntax

    The filter must use MongoDB query operators like $gt inside an object: {age: {$gt: 30}}.
  2. Step 2: Check update operator

    The update must use an operator like $set to change fields: {$set: {status: 'active'}}.
  3. Final Answer:

    db.collection.updateMany({age: {$gt: 30}}, {$set: {status: 'active'}}) -> Option A
  4. Quick Check:

    Use $gt in filter and $set in update = A [OK]
Hint: Use $set to update fields, $gt for greater than in filter [OK]
Common Mistakes:
  • Omitting $set operator in update
  • Using invalid filter syntax like age: > 30
  • Using $update instead of $set
3. Given the collection users with documents:
{"name": "Alice", "score": 50}, {"name": "Bob", "score": 40}, {"name": "Carol", "score": 50}

What will be the result of this command?
db.users.updateMany({score: 50}, {$inc: {score: 10}})
medium
A. One document updated, score becomes 60 for Alice only
B. No documents updated because $inc is invalid here
C. Two documents updated, scores become 60 for Alice and Carol
D. All documents updated, scores become 60, 50, 60

Solution

  1. Step 1: Identify matching documents

    The filter {score: 50} matches Alice and Carol only.
  2. Step 2: Understand the update operation

    The $inc operator increases the score field by 10 for each matched document.
  3. Final Answer:

    Two documents updated, scores become 60 for Alice and Carol -> Option C
  4. Quick Check:

    Filter matches 2 docs, $inc adds 10 = B [OK]
Hint: Filter matches 2 docs, $inc adds 10 to each [OK]
Common Mistakes:
  • Thinking $inc only updates one document
  • Assuming $inc is invalid in updateMany
  • Believing all documents update regardless of filter
4. What is wrong with this updateMany command?
db.products.updateMany({price: {$lt: 100}}, {price: 90})
medium
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

  1. 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.
  2. Step 2: Validate filter and collection

    The filter {price: {$lt: 100}} is correct, and collection name products is valid.
  3. Final Answer:

    Missing update operator like $set in the update document -> Option A
  4. 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

  1. Step 1: Use correct update operators together

    To update multiple fields differently, combine $inc and $set inside one update document: {$inc: {...}, $set: {...}}.
  2. 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()}}).
  3. Final Answer:

    db.products.updateMany({category: 'books'}, {$inc: {stock: 5}, $set: {lastUpdated: new Date()}}) -> Option B
  4. Quick Check:

    Combine $inc and $set correctly = D [OK]
Hint: Combine $inc and $set in one update document [OK]
Common Mistakes:
  • Placing fields outside update operators
  • Trying to do math inside $set without $inc
  • Using invalid syntax for date or increment