Deleting documents in MongoDB removes data from your collections. MongoDB provides flexible methods to delete one or multiple documents based on specified criteria.
Use deleteOne()
to remove the first document that matches a filter.
db.students.deleteOne({ name: "Aisha" });
Use deleteMany()
to remove all documents matching the filter.
db.students.deleteMany({ age: { $lt: 20 } });
If you want to remove all documents in a collection, use an empty filter with deleteMany()
:
db.students.deleteMany({});
{}
.
deleteOne(filter)
– Deletes the first matching document.deleteMany(filter)
– Deletes all matching documents.{}
with deleteMany
to clear a collection.find()
with the same filter first can help you see what will be deleted.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!