Lesson 17 of 50 – Deleting Documents in MongoDB
34%

Deleting Documents in MongoDB

In MongoDB, deleting means removing existing documents from a collection. For example, we can remove a student record, delete products that are no longer available, or remove old records. MongoDB provides methods such as deleteOne() and deleteMany().

1. What Is Deleting a Document?

A MongoDB document stores data in key-value pairs. When we delete a document, that record is removed from its collection.

Example document in the students collection:

{
    _id: 1,
    name: "Rahul",
    age: 20,
    course: "Java",
    fee: 5000
}

If Rahul leaves the course, we can delete his document using a suitable filter.

2. The deleteOne() Method

The deleteOne() method removes the first document that matches the specified filter.

Syntax
db.collection.deleteOne(
    { filter }
)
Example
db.students.deleteOne(
    { _id: 1 }
)

This command deletes the document whose _id is 1.

Delete a Student by Name
db.students.deleteOne(
    { name: "Rahul" }
)

If several documents have the name Rahul, this command removes only the first matching document.

Check the Remaining Documents
db.students.find()

3. The deleteMany() Method

The deleteMany() method removes all documents that match the specified filter.

Syntax
db.collection.deleteMany(
    { filter }
)
Example: Delete All Java Students
db.students.deleteMany(
    { course: "Java" }
)

Every document whose course is Java will be deleted from the students collection.

Example: Delete Students Below a Certain Age
db.students.deleteMany(
    { age: { $lt: 18 } }
)

This deletes all student documents whose age is less than 18.

4. Delete Documents Using Comparison Operators

We can use comparison operators to select documents for deletion.

Delete Products with a Price Below 100
db.products.deleteMany(
    { price: { $lt: 100 } }
)
Delete Employees with a Salary Above 100000
db.employees.deleteMany(
    { salary: { $gt: 100000 } }
)
Delete Inactive Users
db.users.deleteMany(
    { status: "inactive" }
)

The filter determines which documents are removed. Always verify the filter before running a delete command.

5. Delete All Documents from a Collection

Passing an empty filter to deleteMany() removes every document from the collection.

db.students.deleteMany({})

The collection remains, but its documents are removed. This operation cannot be undone automatically.

Warning: Never run deleteMany({}) on important data unless you intentionally want to delete every document. Practice with a test collection.

6. Delete a Collection

To remove an entire collection, use the drop() method.

Example
db.students.drop()

This removes the students collection, including its documents and collection indexes.

Unlike deleteMany({}), which keeps the collection, drop() removes the collection itself.

7. Difference Between deleteOne() and deleteMany()

Feature deleteOne() deleteMany()
Purpose Deletes one matching document Deletes all matching documents
Filter Required to select a record Required to select records
Empty filter Deletes one document if the collection is not empty Deletes all documents in the collection
Example deleteOne({`{ _id: 1 }`}) deleteMany({`{ course: "Java" }`})

8. Important Points to Remember

  • deleteOne() deletes at most one matching document.
  • deleteMany() deletes every matching document.
  • Use a specific filter when deleting individual records.
  • deleteMany({}) deletes all documents in the collection.
  • drop() removes the collection itself.
  • Deletion commands do not provide a normal undo operation.
  • Check important records before deleting them.

9. Practical Assignment

Create a collection named practice_delete and insert these sample documents:

db.practice_delete.insertMany([
    {
        _id: 1,
        name: "Rahul",
        age: 20,
        course: "Java"
    },
    {
        _id: 2,
        name: "Priya",
        age: 17,
        course: "Python"
    },
    {
        _id: 3,
        name: "Aman",
        age: 22,
        course: "Java"
    },
    {
        _id: 4,
        name: "Neha",
        age: 16,
        course: "HTML"
    }
])
Complete these tasks:
  1. Display all documents using find().
  2. Delete Rahul's document using deleteOne().
  3. Delete all students whose age is less than 18.
  4. Delete all students whose course is Java.
  5. Display the remaining documents.
  6. Explain the difference between deleteOne() and deleteMany().

Run the insertion command only once, or clear the test collection before inserting the same sample data again.

🧠 Quick Quiz

1. Which method deletes all documents matching a filter?




2. What does db.students.drop() do?




3. Which method deletes at most one matching document?