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().
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.
The deleteOne() method removes the first
document that matches the specified filter.
db.collection.deleteOne(
{ filter }
)
db.students.deleteOne(
{ _id: 1 }
)
This command deletes the document whose
_id is 1.
db.students.deleteOne(
{ name: "Rahul" }
)
If several documents have the name Rahul, this command removes only the first matching document.
db.students.find()
The deleteMany() method removes all
documents that match the specified filter.
db.collection.deleteMany(
{ filter }
)
db.students.deleteMany(
{ course: "Java" }
)
Every document whose course is Java will be deleted
from the students collection.
db.students.deleteMany(
{ age: { $lt: 18 } }
)
This deletes all student documents whose age is less than 18.
We can use comparison operators to select documents for deletion.
db.products.deleteMany(
{ price: { $lt: 100 } }
)
db.employees.deleteMany(
{ salary: { $gt: 100000 } }
)
db.users.deleteMany(
{ status: "inactive" }
)
The filter determines which documents are removed. Always verify the filter before running a delete command.
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.
deleteMany({}) on important data unless
you intentionally want to delete every document.
Practice with a test collection.
To remove an entire collection, use the
drop() method.
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.
| 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" }`}) |
deleteOne() deletes at most one matching document.deleteMany() deletes every matching document.deleteMany({}) deletes all documents in the collection.drop() removes the collection itself.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"
}
])
find().deleteOne().deleteOne() and deleteMany().Run the insertion command only once, or clear the test collection before inserting the same sample data again.
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?