MongoDB mongosh Delete
MongoDB mongosh – Delete Documents 🗑️
Be careful ⚠️—deleted data cannot be recovered unless you have backups.
1️⃣ Delete a Single Document (deleteOne)
Syntax
Example
Output
📌 Deletes only the first matching document.
2️⃣ Delete Multiple Documents (deleteMany)
Deletes all documents matching the condition.
3️⃣ Delete All Documents in a Collection ⚠️
✅ Collection remains
❌ All documents removed
4️⃣ Delete Using Conditions
Greater than / Less than
Logical Operator
5️⃣ Delete by _id
📌 _id is unique → deletes exactly one document.
6️⃣ Check Before Deleting (Best Practice 👍)
Always preview data before deleting:
Then delete:
7️⃣ Drop Collection (Deletes Structure + Data)
Output:
⚠️ Collection is permanently removed.
SQL vs MongoDB (DELETE)
SQL
MongoDB
Common Mistakes ⚠️
-
Using
{}accidentally (deletes everything) -
Forgetting filter condition
-
Confusing
deleteMany()withdrop()
