Chapter 9: MongoDB mongosh Delete

MongoDB mongosh Delete — the D in CRUD.

This is how you permanently remove documents from a collection. Once deleted, they’re gone (unless you have backups or oplog/time-travel — but that’s advanced).

1. The Two Main Delete Methods in mongosh (2026 Standard — Official & Recommended)

Method What it does How many documents deleted? Typical return value Best for
db.collection.deleteOne(filter, options) Deletes the first document that matches the filter At most 1 { acknowledged: true, deletedCount: 0 or 1 } Precise removal: delete one specific wish, one user by _id, one broken record
db.collection.deleteMany(filter, options) Deletes all documents that match the filter 0 to many { acknowledged: true, deletedCount: N } Bulk cleanup: remove all old wishes, delete all from one sender, clear expired entries

Very important teacher warnings:

  • There is no “undo” button — be very careful with filters (especially empty {}).
  • The old db.collection.remove() method is deprecated since ~2015–2016 and gives warnings in mongosh. → Always use deleteOne() or deleteMany() in 2026.
  • If no documents match the filter → deletedCount: 0 (silent success, no error).
  • Deletion is permanent on that node — but in replica sets, it replicates to secondaries.

2. Hands-on Examples — Using Our valentines2026 Database

Assume we still have db.wishes with these documents (from earlier lessons):

JSON

First, always good practice: preview what you’ll delete with find().

JavaScript

Example 1: deleteOne() — Remove one specific document

JavaScript

Output example:

JSON

→ Only the first matching wish (Rahul → Priya) is gone. If multiple Rahul → Priya existed, only one would be deleted.

Safer way (using unique _id):

JavaScript

→ Always safest when you know the exact _id.

Example 2: deleteMany() — Bulk delete (very powerful, be careful!)

JavaScript

Output example:

JSON

→ Both Rahul wishes gone (the one to Priya and the group one).

Example 3: Delete everything (Danger zone — use with caution!)

JavaScript

Output:

JSON

→ Collection is now empty (but still exists). Never run this accidentally in production!

Example 4: Delete with comparison / complex filter

JavaScript

Example 5: Delete one and return the deleted document (findOneAndDelete)

Sometimes you want to see what was deleted:

JavaScript

Output: Returns the deleted document itself (or null if none found).

JSON

→ Useful in workflows where you need the old data before removal.

3. Quick Cheat Sheet Table

Goal Method Filter example Typical Command Safety Tip
Delete one specific document deleteOne { _id: ObjectId(“…”) } deleteOne({ from: “Rahul”, to: “Priya” }) Use _id when possible
Delete all from one sender deleteMany { from: “Rahul” } deleteMany({ from: “Rahul” }) Preview with find() first
Delete low-quality entries deleteMany { rating: { $lt: 6 } } deleteMany({ rating: { $lt: 6 } })
Clear entire collection (careful!) deleteMany {} deleteMany({}) Think twice — irreversible
Delete & see what was removed findOneAndDelete { email: “old@ex.com” } findOneAndDelete({ … }) Returns the doc
Delete with write concern (safer) Add { writeConcern: { w: “majority” } } Waits for replica confirmation

4. Mini Exercise — Try Right Now!

  1. use valentines2026
  2. Preview: db.wishes.find({ from: “Rahul” }).pretty()
  3. Delete one Rahul wish: db.wishes.deleteOne({ from: “Rahul” })
  4. Delete the rest from Rahul: db.wishes.deleteMany({ from: “Rahul” })
  5. Check remaining: db.wishes.find().pretty()
  6. (Optional — careful) Clear all: db.wishes.deleteMany({}) then show collections (collection still exists, just empty)

Understood beta? Deleting is powerful but emotional — always preview with find() first, like checking if you’re really ready to let go.

Next class options:

  • Drop collection or drop database (when you want to remove everything permanently)?
  • findOneAndDelete vs deleteOne deep dive?
  • Combining all CRUD in a small Valentine Message Board script?
  • Indexing so finds/deletes are fast?
  • Or move to Node.js/Python connection with real code?

Tell me what your heart desires next — class is continuing! 🚀❤️

Any doubt about delete? Ask freely — no broken hearts here! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *