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 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.
mongodump --version # e.g. mongodump version: 100.12.0mongodump 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 |
mongodump --uri="$MONGODB_URI" --oplog --gzip --archive=full-$(date +%F).gz
mongorestore --uri="$MONGODB_URI" --gzip --archive=full-2026-03-09.gz --oplogReplay --dropmongorestore 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.
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.
# 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.
| 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.
mongodump.--drop into a populated collection and stopping at the first duplicate key error.--readPreference=secondary.What does `mongodump --oplog` add to a backup?
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.Next lesson: Replication and Sharding Concepts — how MongoDB stays available through failures and scales beyond one machine.