UpdateMany
UpdateMany: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 15 of 100
UpdateMany
Foundations & CRUD → Queries & Schema → Aggregation & Scale → Atlas & Projects
Foundations & CRUD · 1 — Documents · ~6 min · MongoDB — CRUD Operations
What is this?
updateMany applies the same update to every document matching the filter. Use it for bulk corrections, migrations, and flag flips.
Why should you care?
A festival sale might set discountPercent on all accessories, or a security fix sets mustResetPassword for a cohort of users.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.products.insertMany([
{ name: "Mouse", category: "accessories", price: 599, sale: false },
{ name: "Pad", category: "accessories", price: 299, sale: false },
{ name: "SSD", category: "storage", price: 4000, sale: false }
])
db.products.updateMany(
{ category: "accessories" },
{ $set: { sale: true, discountPercent: 10 } }
)
db.products.find({ sale: true })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Only accessories match the filter.
- Both mouse and pad get sale true and discountPercent 10.
- SSD stays unchanged.
- find shows the two sale items.
Practice next
- Seed the three products.
- Run updateMany and note modifiedCount.
- Verify storage product still has sale false.
- Increase all sale prices with $mul (careful) or re-$set prices.
- Add a second condition price: { $lt: 1000 }.
Remember
updateMany updates all matches. Filters must be precise. Ideal for bulk business rules.
Festival sale toggle
Flipkart merchandising enables sale:true on thousands of SKUs in one category overnight.
Outcome: Storefront reflects discounts without redeploying the app.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!