MONGODB Tutorial



MongoDB DELETE


🗑️ MongoDB Delete Documents

Deleting documents in MongoDB removes data from your collections. MongoDB provides flexible methods to delete one or multiple documents based on specified criteria.

🔸 Delete One Document

Use deleteOne() to remove the first document that matches a filter.

db.students.deleteOne({ name: "Aisha" });
  

🔸 Delete Multiple Documents

Use deleteMany() to remove all documents matching the filter.

db.students.deleteMany({ age: { $lt: 20 } });
  

⚠️ Delete All Documents

If you want to remove all documents in a collection, use an empty filter with deleteMany():

db.students.deleteMany({});
  
Important: Deletion is permanent! Be cautious when running delete commands, especially with broad filters like {}.

✔️ Quick Recap:

  • deleteOne(filter) – Deletes the first matching document.
  • deleteMany(filter) – Deletes all matching documents.
  • Use empty filter {} with deleteMany to clear a collection.
💡 Tip: Always double-check your filter before deleting data. Using find() with the same filter first can help you see what will be deleted.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review