DEV Community

Cover image for How a Single MongoDB Typo Wiped My Production Database (3 Times!) – And How to Avoid It
Hazem Kastouri
Hazem Kastouri

Posted on • Edited on

How a Single MongoDB Typo Wiped My Production Database (3 Times!) – And How to Avoid It

When User.remove(userId) silently deletes everything, and why MongoDB won't warn you

I stared at my empty users collection in horror. For the seventh time. My 'minor bug fix' had just nuked every customer account in production – and MongoDB didn't even blink. Here's how a missing {} cost me days of recovery work, and how you can bulletproof your database.

The Silent Killer

// What I wrote (DANGER):
await User.remove(userId); 

// What I meant (SAFE):
await User.remove({ _id: userId });
Enter fullscreen mode Exit fullscreen mode

Why This Happened (3 Times!)

  1. No Query Validation: MongoDB prioritizes flexibility over safety
  2. Production Blindspots: No backups → no quick recovery
  3. Assumption Trap: "It's just a delete, how bad could it be?"

My 3-Part Survival Kit

  1. Backup Armor
    Automated backups every 15 minutes (shoutout to mongodump cron jobs)
    Pro Tip: Test your restore process before disaster strikes

  2. Paranoid Sanitization

// Safety Wrapper Example
const safeDelete = async (collection, id) => {
  if (!id || typeof id !== 'string') throw new Error("Invalid ID format");
  return collection.deleteOne({ _id: new ObjectId(id) }); // Explicit is better
};
Enter fullscreen mode Exit fullscreen mode
  1. Nuclear Option Prevention
  • Renamed collections to users_prod/users_staging

  • Added middleware logging all bulk deletions

  • Switched to deleteOne() for explicit single-doc ops

Why MongoDB "Allows" This
MongoDB's philosophy:

Flexible queries > strict validation

Historic behavior (changing would break legacy code)

Your responsibility: Validate inputs

Key Takeaways
✅ Always wrap IDs in query objects: { _id: value }
✅ Prefer deleteOne() over deleteMany() for this kind of cases
✅ Backup like your business depends on it (because it does)

Top comments (0)