← Back to MongoDB Course | Chapter 4: CRUD — Update | Lesson 2 of 7

updateMany() का उपयोग

updateMany filter match करने वाला हर document बदलता है।

In this page:

  1. updateMany()
Syntax
javascript
db.collection_name.updateMany(
  filter,
  { $set: { field: new_value } }
)

updateMany()

old records mark करने या एक price increase apply करने जैसे bulk changes के लिए updateMany इस्तेमाल करें। एक empty filter {} सारे documents match करता है, इसलिए चलाने से पहले filters double check करें। Result देता है कितने documents matched और changed हुए।

Note: कौन-से documents बदलेंगे यह preview करने के लिए पहले find से filter चलाएँ।

उदाहरण: updateMany()

bash
test> db.products.insertMany([
...   { _id: 1, name: "Pen", price: 3, category: "office" }, { _id: 2, name: "Desk", price: 150, category: "furniture" },
...   { _id: 3, name: "Notebook", price: 8, category: "office" }
... ])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.products.updateMany({ category: "office" }, { $set: { onSale: true } })
{ acknowledged: true, matchedCount: 2, modifiedCount: 2 }
test> db.products.find({ onSale: true }, { name: 1, _id: 0 })
[ { name: 'Pen' }, { name: 'Notebook' } ]

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. accidentally एक empty filter इस्तेमाल करना
  2. operators भूल जाना
  3. affected documents preview न करना
चैप्टर सारांश
  • updateMany सारे matches प्रभावित करता है
  • {} सब कुछ match करता है
  • पहले find से preview करें
  • Result matched और modified counts report करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.