Lesson 11 / 23
deleteOne & deleteMany
Remove documents with deleteOne and deleteMany, and avoid accidentally deleting a whole collection.
deleteOne()
Removes the first document matching the filter.
db.users.deleteOne({ name: "Alan Turing" })
Output:
{ acknowledged: true, deletedCount: 1 }deleteMany()
Removes every document matching the filter.
db.users.deleteMany({ role: "intern" })
Output:
{ acknowledged: true, deletedCount: 3 }Careful with an empty filter
deleteMany({}) removes every document in the collection — always double-check the filter before running it.