Deleting Documents
6 min read
deleteOne and deleteMany
deleteOne removes the first document matching the filter. deleteMany removes all matching documents. Both return an acknowledged flag and deletedCount.
js
// Delete a specific student
db.students.deleteOne({ name: "Karan Mehta" });
// Delete all unenrolled students
db.students.deleteMany({ enrolled: false });
// Delete ALL documents in the collection (use with caution!)
db.students.deleteMany({});Passing {} as a filter to deleteMany wipes the entire collection. Double-check your filter before running in production.
Ready to test yourself?
Practice MongoDB— quiz & coding exercises