Backup, Restore, Import and Export

Advanced
12 min

Backup, Restore, Import and Export

A database you cannot restore is a database you do not have. MongoDB ships command-line tools for logical backups (mongodump, mongorestore) and for moving data in text formats (mongoexport, mongoimport), and Atlas adds managed snapshots with point-in-time recovery. After this lesson you will be able to back up and restore databases and collections, load and export JSON and CSV, choose a backup strategy for a given deployment size, and avoid the mistakes that make a backup useless on the day you need it.

The Database Tools

The tools are a separate download ("MongoDB Database Tools"), installed alongside the server on Linux packages and available for macOS and Windows. Each accepts the same --uri connection string as mongosh, including credentials and authSource, and works against local servers, replica sets and Atlas.

bash
mongodump --version # e.g. mongodump version: 100.12.0

mongodump and mongorestore

mongodump reads collections and writes BSON files plus a metadata.json per collection that records indexes and collection options, so a restore recreates them. Useful flags:

| Flag | Purpose | |---|---| | --db, --collection | limit the dump; omit both for all databases | | --query='{ "placedAt": { "$gte": { "$date": "2026-01-01T00:00:00Z" } } }' | dump a subset (Extended JSON) | | --out=./dump or --archive=file | directory tree or a single archive file | | --gzip | compress on the fly | | --oplog | replica sets only: capture oplog entries during the dump for a consistent point-in-time snapshot (requires dumping all databases) | | --readPreference=secondary | take the load off the primary |

bash
mongodump --uri="$MONGODB_URI" --oplog --gzip --archive=full-$(date +%F).gz mongorestore --uri="$MONGODB_URI" --gzip --archive=full-2026-03-09.gz --oplogReplay --drop

mongorestore inserts the documents and rebuilds the indexes. --drop removes each target collection first (otherwise existing documents cause duplicate key errors), --nsFrom/--nsTo rename namespaces, --noIndexRestore skips index builds when you plan to create them later, and --dryRun validates without writing. Users and roles are restored only from a dump of the admin database with --restoreDbUsersAndRoles.

mongoexport and mongoimport

These tools speak JSON and CSV for humans and other systems, not BSON. Types are preserved only through Extended JSON ({"$oid": ...}, {"$date": ...}); CSV loses them entirely, so they are the wrong tools for backups and the right tools for data exchange.

bash
# CSV export of selected fields mongoexport --uri="$URI/shop" --collection=products --type=csv --fields=sku,name,price --out=products.csv # CSV import with a header row, upserting on sku mongoimport --uri="$URI/shop" --collection=products --type=csv --headerline \ --file=products.csv --mode=upsert --upsertFields=sku # JSON Lines import (one document per line) into a fresh collection mongoimport --uri="$URI/shop" --collection=events --file=events.ndjson --drop

--mode accepts insert (default), upsert, merge (update existing fields, keep the rest) and delete. --columnsHaveTypes with headers such as price.double() casts CSV columns during import.

Choosing a Backup Strategy

| Method | Best for | Notes | |---|---|---| | mongodump / mongorestore | small to medium datasets, migrations, dev copies | logical; slow beyond a few hundred GB; sharded clusters need extra care | | Filesystem or volume snapshots | large self-managed deployments | consistent when journaling is on; snapshot every member's volume | | Atlas Cloud Backups | any Atlas cluster | scheduled snapshots plus continuous point-in-time restore to any second within the window | | Ops Manager / Cloud Manager | enterprise self-managed | managed snapshots and PITR for self-hosted clusters |

Whatever the method, back up on a schedule, keep copies off the database host (ideally in another region), encrypt archives, define a retention policy, and test restores regularly — restore into a scratch database and run a few queries.

Common Mistakes

  • Treating a JSON export as a backup. Types drift and indexes are not captured; use mongodump.
  • Restoring without --drop into a populated collection and stopping at the first duplicate key error.
  • Dumping a busy primary at peak hours without --readPreference=secondary.
  • Never rehearsing a restore, discovering missing credentials or an incompatible tool version during an outage.
Quick Quiz
Question 1 of 3

What does `mongodump --oplog` add to a backup?

Key Takeaways

  • mongodump and mongorestore produce and consume BSON archives with index metadata; add --gzip, --archive and --oplog for production use.
  • mongorestore --drop replaces existing collections; --nsFrom/--nsTo restore into a different database name.
  • mongoexport and mongoimport move JSON and CSV for interchange, with --mode=upsert for incremental loads; they are not backups.
  • Pick the strategy by data size and recovery objectives: logical dumps, volume snapshots, or Atlas Cloud Backups with point-in-time restore.
  • Store backups off-host and encrypted, and rehearse restores on a schedule.

Next lesson: Replication and Sharding Concepts — how MongoDB stays available through failures and scales beyond one machine.

Backup, Restore, Import and Export - MongoDB | CodeYourCraft | CodeYourCraft