deleteMany() का उपयोग
deleteMany filter match करने वाला हर document remove करता है।
In this page:
Syntax
db.collection_name.deleteMany(filter)
deleteMany()
expired sessions remove करने जैसे cleanup के लिए deleteMany इस्तेमाल करें। एक empty filter सारे documents delete करता है लेकिन collection और इसके indexes छोड़ देता है, drop के उलट। पहले countDocuments से filter preview करें।
Note:
delete करने से पहले same filter के साथ countDocuments चलाएँ।
उदाहरण: deleteMany()
test> db.sessions.insertMany([{ _id: 1, expired: true }, { _id: 2, expired: false }, { _id: 3, expired: true }])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.sessions.countDocuments({ expired: true })
2
test> db.sessions.deleteMany({ expired: true })
{ acknowledged: true, deletedCount: 2 }
test> db.sessions.find()
[ { _id: 2, expired: false } ]
⚠️ 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 {} pass करना
- deleteMany({}) को drop से confuse करना
- पहले filter test न करना
चैप्टर सारांश
- deleteMany सारे matches remove करता है
- {} सब कुछ remove करता है लेकिन collection रखता है
- countDocuments से preview करें
- deletedCount return करता है
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: