MongoDB deleteOne() Method (original) (raw)

Last Updated : 5 May, 2026

The deleteOne() method in MongoDB removes a single document from a collection that matches a specified filter. It is useful for deleting a specific document based on given criteria, helping maintain the integrity and relevance of the data in the collection.

Syntax

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

Return value of deleteOne()

This method returns a document that contains the following fields:

Examples of MongoDB deleteOne() Method

Some examples of the deleteOne method in MongoDB. In the following examples, we are working with:

Screenshot-2026-02-05-113043

Example 1: Deleting the First Document in a Collection

Deleting first document from the student collection using an empty filter {}:

**Query:

db.student.deleteOne({})

**Output:

Screenshot-2026-02-05-114743

This will remove the first document in the student collection, irrespective of its content.

Example 2: Delete One Document that Match Selection Criteria

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

**Query:

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

**Output:

Screenshot-2026-02-05-113335

Example 3: Deleting One Document When Multiple Documents Match the Filter

Deleting a document from the student collection. Here, two documents match the filter (i.e., branch: "CSE"), so this method deletes the first document among them.

**Query:

db.student.deleteOne({ branch: "CSE" })

**Output:

Screenshot-2026-02-05-113628

In this case, there are two documents with branch: "CSE", so only the first one is deleted.

Example 4: Deleting a Document from the 'student' Collection Based on Age

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

**Query:

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

**Output:

Screenshot-2026-02-05-114119

Example 5: Using Collation for String Comparison

To delete a document with case-insensitive matching, you can specify collation:

**Query:

db.student.deleteOne({ name: "clevin" }, { collation: { locale: "en", strength: 2 } })

**Output:

Screenshot-2026-02-05-115617

Example 6: Specifying a Hint for Index Optimization

For collections with large datasets, specifying a hint can improve performance:

**Query:

db.contributor.deleteOne(
{ name: "Lee" },
{ hint: "name_index" }
)

**Output:

Screenshot-2026-02-05-120148

This command ensures that MongoDB uses the name_index index to optimize the query for deleting the document.

Common Errors and their Solutions

Understanding these common issues and their solutions can help you avoid unnecessary complications and ensure smooth operations.

1. Using deleteOne on a Capped Collection

Capped collections are fixed-size collections that do not allow deletions. Attempting to delete a document from a capped collection will result in an error.

2. Deleting Unintended Documents

If the filter criteria are too general or incorrect, deleteOne might delete a document you did not intend to remove.

3. Performance Issues with Large Collections

Without an index, MongoDB will examine every document in the collection to find matches, resulting in slower performance.

Important Points for Using deleteOne()

Here are some important points to be remembered: