Recall & Review
beginner
What command deletes all documents in a MongoDB collection?
Use
db.collection.deleteMany({}) to delete all documents in the collection.Click to reveal answer
beginner
What does the empty filter
{} mean in deleteMany({})?It means "match all documents" so every document in the collection will be deleted.
Click to reveal answer
intermediate
How is
deleteMany({}) different from drop() in MongoDB?deleteMany({}) removes all documents but keeps the collection. drop() deletes the entire collection and its indexes.Click to reveal answer
intermediate
What happens if you run
db.collection.deleteOne({}) with an empty filter?It deletes only one document that matches the empty filter, so effectively it deletes one random document.
Click to reveal answer
beginner
Why should you be careful when using
deleteMany({})?Because it deletes all documents in the collection, which can cause data loss if done unintentionally.
Click to reveal answer
Which MongoDB command deletes all documents in a collection but keeps the collection itself?
✗ Incorrect
deleteMany({}) deletes all documents but keeps the collection. drop() deletes the entire collection.What does the filter
{} mean when used in deleteMany({})?✗ Incorrect
An empty filter
{} matches all documents, so all documents are deleted.What is the effect of
db.collection.deleteOne({})?✗ Incorrect
deleteOne({}) deletes one document matching the filter, here the empty filter matches any document.Which command removes the entire collection including its indexes?
✗ Incorrect
drop() deletes the whole collection and its indexes.Why should you be cautious when using
deleteMany({})?✗ Incorrect
deleteMany({}) deletes all documents, which can cause data loss if used by mistake.Explain how to delete all documents in a MongoDB collection without deleting the collection itself.
Think about the command that removes documents but not the collection.
You got /3 concepts.
Describe the difference between
deleteMany({}) and drop() in MongoDB.One removes data, the other removes the whole container.
You got /3 concepts.