Sitemap
Vishesh Learn

Vishesh Learn

A forever learner, a computer enthusiast and an artist, who loves creating user experience using his artistic and technical skills.

Prevent Data Loss in MongoDB: A Production Disaster You Can Avoid

2 min read3 days ago

--

Press enter or click to view image in full size
One overlooked test script can wipe out your production database. | Illustration: AI-generated using OpenAI ImageGen.
One overlooked test script can wipe out your production database. | Illustration: AI-generated using OpenAI ImageGen.

Imagine this: Your production database has 1,000+ real users. The app has been running successfully for months. One day, you ask an AI agent to fix a small bug. It runs your test scripts to check for potential issues.

One of those scripts contains a line that cleans up the users collection before every test run.

And just like that, your production users are gone.

How did it happen?

During development, someone on the team created test scripts to automate authentication testing.

To avoid accumulating test users, they added a cleanup command that deletes existing users before each test run.

It worked perfectly during development. Nobody thought much about it.

Months later, with the app already in production and real users actively using it, the same script was accidentally executed against the production database.

You try logging in. No user exists.

You check MongoDB. Only two test users remain.

You refresh the collection, check filters, inspect the admin panel, and search for any possible explanation.

Eventually, you realise the truth: your production data is gone. What started as a small bug fix has turned into a major production incident.

How could this have been prevented?

Here are some practices that can save you from a similar disaster.

1. Enable automated backups

Enable backups on MongoDB Atlas or implement your own backup system.

Get Vishesh Learn’s stories in your inbox

Join Medium for free to get updates from this writer.

Do not assume backups are enabled by default.

2. Keep offline database backups

Use MongoDB Database Tools to export your database and store backups in multiple locations, such as:

  • Local storage
  • External drives
  • Google Drive or other cloud storage

3. Separate development and production databases

Always use separate connection strings for development and production.

Your production connection string should never exist in your local development environment or in environments accessible to AI agents.

4. Review destructive operations

Be careful with commands such as deleteMany(), drop(), and collection cleanup scripts.

Never allow test scripts to run destructive operations against production data.

MongoDB backup and restore

First, install MongoDB Database Tools.

Then use mongodump to export your database:

mongodump --uri "mongodb+srv://<username>:<password>@<cluster-name>.<unique-string>.mongodb.net/<db>"

To restore a backup, use mongorestore:

mongorestore --uri "mongodb+srv://<username>:<password>@<cluster-name>.<unique-string>.mongodb.net/<db>" <dump-folder>

Refer to the official documentation:

MongoDB Database Tools

Remember: A small mistake can wipe out years of production data. Automated backups, database isolation, and careful testing are not optional precautions. They are essential parts of building reliable applications.

--

--

Vishesh Learn

Written by Vishesh Learn

0 followers

A forever learner, a computer enthusiast and an artist, who loves creating user experience using his artistic and technical skills.