Skip to main content

How Do I Backup A Collection In MongoDB?

by
Last updated on 4 min read

Quick Fix Summary

Run mongodump with --collection and --db to back up a specific collection. Restore with mongorestore --collection. For remote servers, add --host and --port. Always test backups with mongorestore --dryRun before trusting them.

What's Happening

MongoDB keeps collections as groups of documents inside a database. A backup copies those documents so you can bring them back if something goes wrong—like data corruption or an accidental delete. The mongodump tool turns your collections into BSON files (binary snapshots). Later, you can bring those files back to life with mongorestore.

As of 2026, MongoDB 7.0 is the latest stable release, and mongodump/mongorestore are still the go-to tools for logical backups. Just make sure you use the same—or newer—version of these tools when restoring to dodge compatibility headaches.

How do I backup a single collection in MongoDB?

Use the mongodump command with --db and --collection flags.

Open your terminal and run:

mongodump --db yourdb --collection yourcollection --out /backups/mongo/

That command creates a yourdb folder inside /backups/mongo/, with a yourcollection.bson file inside it. (Honestly, this is the simplest way to grab just what you need.)

How do I verify my backup worked without risking anything?

Run mongodump --dryRun first.

Add --dryRun to your command to check everything’s accessible:

mongodump --db yourdb --collection yourcollection --dryRun

It won’t write any files, but it tells you if the collection exists and the command runs cleanly. That way, you know the real backup will work when you actually need it.

How do I restore a backed-up collection?

Use mongorestore with the --drop flag.

When you’re ready to bring the collection back, run:

mongorestore --db restored_db --collection yourcollection --drop /backups/mongo/yourdb/yourcollection.bson

The --drop option wipes the target collection first so you don’t get duplicate conflicts. (Just make sure you’re restoring to the right place.)

What if I’m not sure which collection is causing issues?

Back up the entire database instead.

Run a full backup to capture everything:

mongodump --db yourdb --out /backups/mongo/full_backup_2026/

That gives you a complete snapshot, so you don’t have to guess which collection might be the problem.

How do I back up a collection on a remote MongoDB server?

Use the --uri flag with connection details.

Instead of separate flags, bundle everything into a URI:

mongodump --uri="mongodb://user:password@remotehost:27017/yourdb?authSource=admin" --collection yourcollection --out /backups/mongo/

That single line handles the host, port, authentication, and your target collection—no extra steps needed.

Can I export a collection as JSON instead of BSON?

Yes, use mongoexport for JSON output.

For smaller collections, JSON can be easier to read and edit:

mongoexport --db yourdb --collection yourcollection --out /backups/mongo/yourcollection.json

Later, bring it back with mongoimport. Just remember: JSON is slower and takes more space than BSON.

How do I restore a JSON backup?

Use mongoimport with --drop.

Run this to bring your JSON backup back into MongoDB:

mongoimport --db restored_db --collection yourcollection --drop --file /backups/mongo/yourcollection.json

The --drop flag clears the target collection first, so you don’t end up with duplicates.

What’s the simplest way to automate backups?

Set up a cron job (Linux/macOS) or Task Scheduler (Windows).

Here’s a daily cron job running at 2 AM:

0 2 * * * mongodump --db yourdb --collection yourcollection --out /backups/mongo/$(date +\%Y-\%m-\%d)

Each backup lands in a folder named with the current date, so you always know which version you’re grabbing.

How do I store backups safely offsite?

Copy them to cloud storage or a secondary server.

Use tools like rclone or AWS CLI to move backups off your main machine:

rclone copy /backups/mongo/ remote:bucket-name/mongo_backups/

That way, if your primary server dies, you still have a clean copy somewhere else.

Should I encrypt my backup files?

Absolutely—especially if the data is sensitive.

Encrypting backups keeps them safe from prying eyes. Try gpg:

gpg --encrypt --recipient your@email.com /backups/mongo/yourdb/yourcollection.bson

You’ll need the recipient’s public key installed, but once it’s set up, encryption is automatic.

How often should I test my backups?

At least once every few months.

Don’t just assume your backups work—actually restore one to a test environment. Log any errors and set up alerts for failed backups. (You’d be surprised how often backups look fine until you really need them.)

What MongoDB version should I use for backups?

Stick with MongoDB 7.0 or newer.

As of 2026, MongoDB 7.0 is the current stable release. Always use matching—or newer—versions of mongodump and mongorestore to avoid compatibility issues when restoring.

Where can I find the official MongoDB docs for mongodump?

That page covers every flag, option, and edge case you might run into with mongodump.

What does MongoDB recommend for backup best practices?

The MongoDB Blog stresses automation, testing, and encryption.

They’re pretty clear: automated backups aren’t optional in production. Test them regularly and encrypt anything sensitive.

Does the IRS have rules about backup encryption?

Yes—see IRS Publication 1075 (as of 2026).

It recommends encrypting backup files and storing them securely to block unauthorized access. (If you handle sensitive data, this isn’t optional.)

Edited and fact-checked by the TechFactsHub editorial team.
David Okonkwo

David Okonkwo holds a PhD in Computer Science and has been reviewing tech products and research tools for over 8 years. He's the person his entire department calls when their software breaks, and he's surprisingly okay with that.