MongoDB Delete Documents Practice Questions with Solutions

Introduction

Deleting documents is an important MongoDB skill because real applications often need to remove outdated, incorrect, or unwanted data. MongoDB provides methods such as deleteOne() and deleteMany() for removing documents. In this chapter, you will practice deleting documents using simple filters, deleting multiple matching records, checking the result, and understanding the difference between deleteOne() and deleteMany(). MongoDB Delete Documents Practice questions with solutions to help you understand the concepts.

Q1. Delete One Student Document

Problem Statement

Delete the student whose name is Rahul from the students collection.

MongoDB Command / Query

db.students.deleteOne({ name: "Rahul" })

Expected Output

{
  acknowledged: true,
  deletedCount: 1
}

Explanation

deleteOne() removes only one document that matches the given condition.


Q2. Delete a Student Using Their Age

Problem Statement

Delete one student whose age is 15.

MongoDB Command / Query

db.students.deleteOne({ age: 15 })

Expected Output

{
  acknowledged: true,
  deletedCount: 1
}

Explanation

MongoDB searches for a document where age is 15 and removes one matching document.


Q3. Delete All Students From a Particular Course

Problem Statement

Delete all students who are enrolled in the Python course.

MongoDB Command / Query

db.students.deleteMany({ course: "Python" })

Expected Output

{
  acknowledged: true,
  deletedCount: 3
}

Explanation

deleteMany() removes all documents matching the filter.

If there are 3 Python students, deletedCount will be 3.


Q4. Delete Students With Age Greater Than 18

Problem Statement

Delete all students whose age is greater than 18.

MongoDB Command / Query

db.students.deleteMany({ age: { $gt: 18 } })

Expected Output

{
  acknowledged: true,
  deletedCount: 2
}

Explanation

The $gt operator means greater than.

So MongoDB deletes every document where:

age > 18

Q5. Delete a Document Using Multiple Conditions

Problem Statement

Delete one student named Aman whose age is 17.

MongoDB Command / Query

db.students.deleteOne({
  name: "Aman",
  age: 17
})

Expected Output

{
  acknowledged: true,
  deletedCount: 1
}

Explanation

Both conditions must match:

  • name must be "Aman"
  • age must be 17

This is an AND condition.


Q6. Delete Students Who Are Not From the Python Course

Problem Statement

Delete all students whose course is not Python.

MongoDB Command / Query

db.students.deleteMany({
  course: { $ne: "Python" }
})

Expected Output

{
  acknowledged: true,
  deletedCount: 4
}

Explanation

$ne means not equal to.

Therefore, MongoDB deletes documents where the course is anything other than "Python".


Q7. Delete Students From Either Java or C++

Problem Statement

Delete all students whose course is either Java or C++.

MongoDB Command / Query

db.students.deleteMany({
  course: { $in: ["Java", "C++"] }
})

Expected Output

{
  acknowledged: true,
  deletedCount: 3
}

Explanation

$in checks whether a field contains one of the specified values.

Here, students from both Java and C++ are deleted.


Q8. Delete Documents Where a Field Exists

Problem Statement

Delete all student documents that contain a temporary field.

MongoDB Command / Query

db.students.deleteMany({
  temporary: { $exists: true }
})

Expected Output

{
  acknowledged: true,
  deletedCount: 2
}

Explanation

$exists: true finds documents where the specified field exists.

So every student document containing the temporary field will be deleted.


Q9. Delete One Document and Check the Collection

Problem Statement

Delete one student named Neha, then display the remaining students.

MongoDB Command / Query

db.students.deleteOne({ name: "Neha" })

db.students.find()

Expected Output

{
  acknowledged: true,
  deletedCount: 1
}

Then find() displays the remaining documents:

[
  {
    name: "Aman",
    age: 17,
    course: "Python"
  },
  {
    name: "Priya",
    age: 16,
    course: "Java"
  }
]

Explanation

First, deleteOne() removes Neha’s document. The second command verifies what remains in the collection with the use of deleteOne()(Delete One Document)


Q10. Delete All Documents From a Collection

Problem Statement

Delete all documents from the students collection without deleting the collection itself.

MongoDB Command / Query

db.students.deleteMany({})

Expected Output

{
  acknowledged: true,
  deletedCount: 5
}

Now check the collection:

db.students.find()

Expected Output

[]

Explanation

An empty filter {} matches every document.

Therefore:

db.students.deleteMany({})

deletes all documents but keeps the students collection available.

Important: Be very careful with deleteMany({}) because it can remove every document in the collection.

Key Takeaways

  • deleteOne() deletes one matching document.
  • deleteMany() deletes all matching documents.
  • deleteOne() can be used when only one record should be removed.
  • deleteMany() is useful for removing multiple records.
  • {} matches all documents.
  • $gt can be used with delete operations.
  • $ne means not equal to.
  • $in matches any value from a specified list.
  • $exists checks whether a field exists.
  • deletedCount tells you how many documents were deleted.
  • deleteMany({}) removes all documents but does not remove the collection.

FAQs

1. What is deleteOne() in MongoDB?

deleteOne() is a MongoDB method used to delete one document that matches a specified filter.

db.students.deleteOne({ name: "Rahul" })

2. What is deleteMany() in MongoDB?

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

db.students.deleteMany({ course: "Python" })

3. What is the difference between deleteOne() and deleteMany()?

deleteOne() removes only one matching document, while deleteMany() removes every document matching the filter.

4. What happens when we use deleteMany({})?

The empty filter {} matches every document, so deleteMany({}) deletes all documents from the collection.

5. Does deleteMany({}) delete the collection?

No. It deletes the documents inside the collection but leaves the collection itself available.

6. What does deletedCount mean in MongoDB?

deletedCount shows the number of documents that were successfully deleted.

For example:

{
  acknowledged: true,
  deletedCount: 3
}

means 3 documents were deleted.

7. Can MongoDB delete documents using query operators?

Yes. MongoDB supports operators such as $gt, $lt, $ne, $in, $exists, and others in delete filters.

For example:

db.students.deleteMany({
  age: { $gt: 18 }
})

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top