updateMany() — MongoDB Technical Interview Questions & Answers
Updates every document matching a filter.
updateMany()
- Simple meaning: updateMany applies the update to every matching document.
- How to use: db.students.updateMany({ city: "Pune" }, { $set: { batch: "A" } }).
- Try this: two Pune students, updateMany, find to verify both changed.
- Common mistake: updateMany({}) in production — updates everyone. Always check the filter.
- Interview tip: Many docs. Contrast updateOne. Empty filter is dangerous.
Technical Interview Tip: updateMany needs a careful filter.
Code Examples for updateMany()
updateMany() — working example
db.users.updateMany(
{ city: "Pune" },
{ $set: { active: true }, $inc: { loginCount: 1 } }
)updateMany() — updateMany applies the update to every matching document.
How to Explain updateMany() to an Interviewer
updateOne vs updateMany?
First match vs every match