← Back to MongoDB Course | Chapter 5: CRUD — Delete | Lesson 2 of 6

deleteMany() का उपयोग

deleteMany filter match करने वाला हर document remove करता है।

In this page:

  1. deleteMany()
Syntax
javascript
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()

bash
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. #}
आम गलतियां
  1. accidentally {} pass करना
  2. deleteMany({}) को drop से confuse करना
  3. पहले 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:

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.