Open In App

MongoDB – db.collection.deleteone()

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

The MongoDB deleteOne() method is an essential tool for removing a single document from a collection that matches a specified filter. It is widely used for precise deletion tasks, ensuring that we can manage your MongoDB collections effectively by removing specific documents based on certain criteria

In this article, We will learn about the MongoDB deleteone() method in detail, covering its syntax, usage examples, and best practices.

What is the MongoDB deleteone() Method?

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.

Key Features of deleteOne() Method:

  • Deletes one document based on the filter criteria.
  • Ensures minimal impact by removing only the first matching document.
  • Helps maintain data integrity by selectively removing irrelevant or outdated documents.
  • Can be used with various query operators to match documents based on complex conditions.

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

demo mongodb database and collection

Example 1: Delete One Document that Match Selection Criteria

Delete the first matched document whose age is 17 from the student collection using the deleteOne() method.

Query:

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

original database and collection

Output:

database and collection after deleteone method

Explanation: The document with age: 17 is removed from the collection. If no document matches, no document is deleted.

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

Delete the first matched document whose age is less than 18 from the student collection using the deleteOne() method.

Query:

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

before deletion

Output:

after deletion

Explanation: The $lt operator is used to specify that we want to delete the first document where age is less than 18. This query ensures that only one document is deleted, even if multiple documents meet the condition.

Conclusion

The MongoDB deleteOne() method is a powerful and precise way to remove a single document from your collections. It is particularly useful when you need to delete specific documents based on clear criteria while maintaining data integrity and performance. By understanding the syntax, optional parameters, and best practices, we can leverage this method to effectively manage our MongoDB data. This method is essential for keeping your MongoDB collections clean and relevant, ensuring optimal database performance.

FAQs

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
  翻译: