updateMany() का उपयोग
updateMany filter match करने वाला हर document बदलता है।
In this page:
Syntax
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()
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. #}
आम गलतियां
- accidentally एक empty filter इस्तेमाल करना
- operators भूल जाना
- 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: