deleteOne()
deleteOne removes the first document that matches the filter.
In this page:
Syntax
db.collection_name.deleteOne(filter)
deleteOne()
db.collection.deleteOne(filter) deletes at most one matching document and returns deletedCount. Use a unique filter such as _id to be sure which document goes. Deletion is permanent unless you have backups.
Note:
Filter by _id when you need to delete a specific document.
Example: deleteOne()
test> db.users.insertMany([{ _id: 1, name: "Ada" }, { _id: 2, name: "Bob" }, { _id: 3, name: "Bob" }])
{ acknowledged: true, insertedIds: { '0': 1, '1': 2, '2': 3 } }
test> db.users.deleteOne({ name: "Bob" })
{ acknowledged: true, deletedCount: 1 }
test> db.users.deleteOne({ name: "Nobody" })
{ acknowledged: true, deletedCount: 0 }
test> db.users.find()
[ { _id: 1, name: 'Ada' }, { _id: 3, name: 'Bob' } ]
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Using deleteOne with a vague filter
- Forgetting deletes cannot be undone
- Passing no filter
Chapter Summary
- deleteOne removes one match
- Returns deletedCount
- Use a unique filter
- Deletion is permanent
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: