The Wayback Machine - https://web.archive.org/web/20250219091041/https://www.geeksforgeeks.org/mongodb-db-collection-deleteone/
Open In App

MongoDB – db.collection.deleteone()

Last Updated : 08 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The deleteOne() method in MongoDB is used to remove a single document from a collection that matches a specified filter.

In this article, We will learn about the MongoDB deleteone() method in detail by understanding the various examples and so on.

MongoDB deleteone()

  • The deleteOne() method in MongoDB is used to remove a single document from a collection that matches a specified filter.
  • This method is particularly useful when we want to delete one specific document based on certain criteria.
  • Using deleteOne() effectively allows us to manage our MongoDB collections by removing specific documents based on our criteria, maintaining the integrity and relevance of our data.

Syntax

db.Collection_name.deleteOne(
selection_criteria:<document>,
{
    writeConcern: <document>,
    collation: <document>,
    hint: <document|string>
})

Parameter

  • Selection criteria:  Specifies criteria to delete documents. The type of this parameter is a document.

Optional Parameters

  • writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is a document.
  • Collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison like rules for lettercase and accent marks. The type of this parameter is a document.
  • hint: It is a document or field that specifies the index to use to support the filter. It can take an index specification document or the index name string and if you specify an index that does not exist, then it will give an error.

Return

This method returns a document that contains the following fields:

  • acknowledged: It is a boolean field, if the value of this field is true then the operation run with write concern. If the value of this field is false, then the operation runs without write concern.
  • deleteCount: This field contains the total number of deleted documents.

Examples of MongoDB deleteOne Method

Let’s look at some examples of the deleteOne method in MongoDB.

In the following examples, we are working with:

Database: gfg

Collections: student

Document: Four documents contains the name and the age of the students

Output:

demo mongodb database and collection

Example 1: Delete One Document that Match Selection Criteria

Delete the first matched document whose age is 17:

db.student.deleteOne({age:17})

Here, we delete the first document that matches the filter query(i.e., age:17) from the student collection using the deleteOne() method.

original database and collection

After deletion:

database and collection after deleteone method

Example 2: Deleting a Document from the ‘student’ Collection Based on Age

Delete the first matched document whose age is less than 18:

db.student.deleteOne({age:{$lt:18}})

Here, we delete the first document that matches the filter query(i.e., {age:{$lt:18}}) from the student collection using the deleteOne() method.

Note: $lt operator means less than.

before deletion

After deletion:

after deletion

Conclusion

The deleteOne() method in MongoDB is a powerful tool for maintaining your database by allowing you to remove specific documents based on defined criteria. By understanding and utilizing its parameters and options, you can ensure efficient and precise data management within your collections.

FAQs on MongoDB deleteone()

Can deleteOne() delete multiple documents?

No, deleteOne() is designed to delete only one document that matches the specified filter. If you need to delete multiple documents, you should use the deleteMany() method.

What happens if no documents match the filter criteria in deleteOne()?

If no documents match the filter criteria, the deleteOne() method will not delete any documents, and the returned document will show deleteCount: 0.

How can I ensure that a specific index is used in the deleteOne() operation?

You can specify the index to be used by including the hint parameter in your deleteOne() method. This can be either an index specification document or the index name string.


Next Article

Similar Reads

three90RightbarBannerImg