MongoDB provides two pairs of tools for data backup and migration: mongodump with mongorestore, and mongoexport with mongoimport. Each serves different purposes and has distinct characteristics.
Mongodump and mongorestore work with BSON format, the native binary format MongoDB uses internally. Mongodump creates a binary backup of databases or collections, preserving all data types, indexes, and collection options. Mongorestore recreates collections from these binary backups, maintaining fidelity to the original data.
Advantages of mongodump and mongorestore include preserving all BSON data types including binary data, dates, and ObjectId that would lose fidelity in JSON, backing up and restoring indexes and collection settings, faster performance for large datasets because binary format is more compact and faster to process, and full database backups including system collections.
Disadvantages include binary format is not human-readable, output is not easily editable, and less portable across different MongoDB versions or external systems.
Mongoexport and mongoimport work with human-readable formats like JSON and CSV. Mongoexport exports collection data to JSON or CSV files. Mongoimport imports data from JSON, CSV, or TSV files into collections.
Advantages of mongoexport and mongoimport include human-readable output that can be viewed and edited in text editors, CSV format integrates easily with spreadsheets and other tools, flexibility to import from external sources not originally from MongoDB, and ability to export selected fields and query results.
Disadvantages include some data types lose precision or cannot be represented in JSON or CSV, indexes and collection settings are not exported, slower performance for large datasets, and potential data type conversion issues.
Use mongodump and mongorestore for full database backups, disaster recovery, migrating entire databases between MongoDB instances, preserving exact data including all types and indexes, and when performance matters for large datasets.
Use mongoexport and mongoimport for data migration to non-MongoDB systems, sharing data with external tools or teams, exporting reports or subsets of data, loading test data or initial datasets, migrating specific collections or query results, and when you need human-readable format for inspection or editing.
Example code
// MONGODUMP / MONGORESTORE (Binary BSON)
// Backup entire database
mongodump --host=localhost --port=27017 \
--db=mydb --out=/backup/
// Creates: /backup/mydb/ with .bson and .metadata.json files
// Backup specific collection
mongodump --db=mydb --collection=users --out=/backup/
// Backup with query filter
mongodump --db=mydb --collection=orders \
--query='{"status": "completed"}' --out=/backup/
// Restore entire database
mongorestore --host=localhost --port=27017 \
--db=mydb /backup/mydb/
// Restore with different name
mongorestore --db=mydb_copy /backup/mydb/
// MONGOEXPORT / MONGOIMPORT (Human-readable)
// Export to JSON
mongoexport --db=mydb --collection=users \
--out=users.json
// Creates readable JSON file
// Export to CSV with specific fields
mongoexport --db=mydb --collection=users \
--type=csv --fields=name,email,age --out=users.csv
// Export with query
mongoexport --db=mydb --collection=orders \
--query='{"total": {"$gt": 100}}' --out=large_orders.json
// Import JSON
mongoimport --db=mydb --collection=users \
--file=users.json
// Import CSV with header
mongoimport --db=mydb --collection=users \
--type=csv --headerline --file=users.csv
// Import with upsert (update or insert)
mongoimport --db=mydb --collection=products \
--file=products.json --mode=upsert \
--upsertFields=productId
// WHEN TO USE EACH:
// mongodump/restore: Production backups, full migrations, preserve everything
// mongoexport/import: Data sharing, CSV integration, subset exports, human-readable