← Back to MongoDB Course | Chapter 5: CRUD — Delete | Lesson 1 of 6

deleteOne()

deleteOne removes the first document that matches the filter.

In this page:

  1. deleteOne()
Syntax
javascript
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()

bash
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
  1. Using deleteOne with a vague filter
  2. Forgetting deletes cannot be undone
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.