Skip to main content

How do you delete a document in the DB?

To delete a document in MongoDB, use deleteOne() and deleteMany().

Deleting one document:

js
db.users.deleteOne({ name: "Alex" })

MongoDB finds the first document matching the filter and deletes it.

Deleting several documents:

js
db.users.deleteMany({ isActive: false })

Every document matching the condition gets deleted.

A note on the logic:

  • the first argument is a filter that selects which documents to delete,
  • deleteOne() deletes only one document,
  • deleteMany() deletes every document that matches.

So, to delete data in MongoDB, it's enough to apply one of these two methods with the right filter.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.