Introduction
The updateMany method changes multiple documents in a collection at once. It helps you fix or add data quickly without changing one by one.
Jump into concepts and practice - no test required
db.collection.updateMany(filter, update, options)
db.users.updateMany({ age: { $lt: 18 } }, { $set: { status: "minor" } })db.products.updateMany({ category: "books" }, { $inc: { stock: 10 } })db.orders.updateMany({}, { $set: { shipped: false } })db.employees.insertMany([
{ name: "Alice", department: "sales", salary: 5000 },
{ name: "Bob", department: "sales", salary: 4500 },
{ name: "Charlie", department: "hr", salary: 4000 }
])
const result = db.employees.updateMany(
{ department: "sales" },
{ $inc: { salary: 500 } }
)
result.modifiedCountupdateMany method do in MongoDB?updateMany method is designed to update all documents that match a given filter in a collection.deleteMany, inserting by insertMany, and finding by findOne. So, only updateMany updates multiple documents.status to active for all documents where age is greater than 30 using updateMany?$gt inside an object: {age: {$gt: 30}}.$set to change fields: {$set: {status: 'active'}}.users with documents:{"name": "Alice", "score": 50}, {"name": "Bob", "score": 40}, {"name": "Carol", "score": 50}db.users.updateMany({score: 50}, {$inc: {score: 10}}){score: 50} matches Alice and Carol only.$inc operator increases the score field by 10 for each matched document.updateMany command?db.products.updateMany({price: {$lt: 100}}, {price: 90}){price: 90} lacks an update operator like $set. MongoDB requires operators to specify how to update fields.{price: {$lt: 100}} is correct, and collection name products is valid.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?$inc and $set inside one update document: {$inc: {...}, $set: {...}}.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()}}).