MongoDB findOneAndDelete() Method (original) (raw)

Last Updated : 5 May, 2026

findOneAndDelete() removes a single document that matches a filter and returns the deleted document, making it useful when you need the removed data for logging or auditing.

Syntax

db.Collection_name.findOneAndDelete( 
Selection_criteria,

projection: , 
sort: ,   
maxTimeMS: ,   
collation:
})

Return Value of findOneAndDelete()

Examples of MongoDB findOneAndDelete()

In the following examples, we are working with:

Screenshot-2026-02-06-175205

Example 1: Delete A Document by Name

Delete the first document where the name is "Denis" from the student collection.

**Query:

db.student.findOneAndDelete({name:"Denis"})

**Output:

Screenshot-2026-02-06-175357

Example 2: Find and Delete the document according to the selection criteria

Delete the first document where age is 17, but sort the matching documents by age in descending order.

**Query:

db.student.findOneAndDelete({age:17},{sort:{age:-1}})

**Output:

Screenshot-2026-02-06-175902

Example 3: When no document matches the filter query.

Delete a document where the name is "Jenny", but no such document exists in the collection.

**Query:

db.student.findOneAndDelete({name: "Jenny"})

**Output:

Screenshot-2026-02-06-180024

Example 4: Delete A Document Using Write Concern

Delete a document based on its _id, specifying a write concern for acknowledgment from the majority of the replica set.

**Query:

db.student.findOneAndDelete(
{ "_id": ObjectId("6985dcc28219911372628ca2") },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)

**Output:

delete

**Note: deleteOne() is a related delete method. Use it when you only need acknowledgment and not the deleted document. Use findOneAndDelete() when you need the deleted document back.

Example 5: Sort And Delete A Document

Sort the documents by name and delete the first document found.

**Query:

db.student.findOneAndDelete({}, { sort: { name: 1 } })

**Output:

Screenshot-2026-02-06-181520

Best Practices for Using MongoDB findOneAndDelete()

Here are some best practices to be followed for better results: