Moving data in and out of MongoDB is essential for backups, migrations, or sharing datasets. In this guide, you’ll learn how to use MongoDB’s built-in tools to import and export data in MongoDB in JSON, CSV, or BSON formats. Let’s get started!
Table of Contents
Prerequisite
- Install MongoDB on your machine.
- Ensure MongoDB’s
bindirectory (e.g.,C:\Program Files\MongoDB\Server\6.0\bin) is added to your system’sPATH. - Have a running MongoDB instance (local or remote).
- Download and install MongoDB Database Tools
Method 1: Using mongoexport and mongoimport
These tools handle JSON or CSV files for individual collections.
Step 1: Export Data with mongoexport
Export a collection to a JSON/CSV file.
Command Syntax:
mongoexport --uri=<connection-string> --collection=<collection-name> --out=<filename>
Example: Export the users collection to users.json:
mongoexport --uri="mongodb://localhost:27017/mydb" --collection=users --out=users.json
This command exports users collection data from the database defined in URI and saves data into users.json file.
Similarly, we can easily export MongoDB data in CSV format as well.
For CSV: Add --type=csv and specify fields with --fields:
mongoexport --host=mongodb.host.com --port=27017 --db=databaseName --collection=collectionName --type=csv --fields=field1,field2,field3 --out outputFileName.csv --noHeaderLine
In the above command, we’ve used different options.
| Option | Description |
| –host | Host name where the MongoDB server is. It can be any hostname like IP Address |
| –port | It defines the port number. The default port MongoDB uses is 27017 |
| –db | The database name |
| –collection | Collection name that you want to export |
| –type | The type of the exported file. |
| –fields | It exports data in the exported files from the given fields only. |
| –out | We can define the output file name. |
| –noHeaderLine | This does not include the field name in the header of each column. |
If you get an unrecognized field ‘snapshot’ during the
mongoexport. It may be due to a mismatched version of mongoexport tool and mongo DB server. In this case, we have to include--forceTableScanparameters.
Step 2: Import Data with mongoimport
Import data from a JSON/CSV file into a collection.
Command Syntax:
mongoimport --uri=<connection-string> --collection=<collection-name> --file=<filename>
Example: Import users.json into MongoDB:
mongoimport --uri="mongodb://localhost:27017/mydb" --collection=users --file=users.json
For CSV: Specify --type=csv and --headerline (if the file has headers):
mongoimport --uri="..." --collection=users --type=csv --headerline --file=users.csv
Method 2: Using mongodump and mongorestore
These tools work with BSON (binary JSON) and are ideal for full database backups.
Step 1: Export Data with mongodump
Export an entire database or specific collections.
Command Syntax:
mongodump --uri=<connection-string> --out=<output-directory>
Example: Backup the mydb database to a backup/ folder:
mongodump --uri="mongodb://localhost:27017/mydb" --out=./backup
If you don’t specify –out option then it will dump in the default directory. The default dump directory is dump. In the above example, the database name is mydb. So the dumped file will be saved like dump/mydb.
Step 2: Import Data with mongorestore
Restore data from a mongodump backup.
Command Syntax:
mongorestore --uri=<connection-string> <backup-directory>
Example: Restore the mydb database:
mongorestore --uri="mongodb://localhost:27017/mydb" ./backup/mydb
If you have dumped in the default directory without specifying –out then you can use your file to be dumped like: .dump/mydb. Don’t forget to add . (dot) before dump directory.
Duplicate date entry mongorestore (Optional)
If you are getting errors like duplicate data entry during mongorestore then, you can use –drop along with existing command.
For example:
mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/your_database_name" --drop
If you have a database name employees then it will drop the employees database. This basically means dropping the existing data and inserting a new one.
Key Differences: mongoexport vs. mongodump
| Feature | mongoexport/mongoimport | mongodump/mongorestore |
|---|---|---|
| Data Format | JSON/CSV | BSON (binary) |
| Use Case | Partial data transfer | Full database backup |
| Speed | Slower | Faster |
| Data Fidelity | May lose BSON types | Preserves all data types |
Pro Tips
- Authentication: Include credentials in the URI for remote databases:
mongodb://username:password@host:port/database. - Exclude Fields: Use
--fields="field1,field2"to export specific fields. - Compression: Add
--gzipto reduce backup size (e.g.,mongodump --gzip).
When to Use Which Tool?
- Use
mongoexport/mongoimportfor:- Sharing CSV/JSON files.
- Migrating specific collections.
- Use
mongodump/mongorestorefor:- Full database backups.
- Preserving data types and indexes.
Final Thoughts
With these tools, you can easily transfer MongoDB data for backups, analysis, or migrations. Practice with sample datasets to master the commands!
Got questions? Let me know in the comments below! 👇
Remember
All the commands given here are well tested in my machine. If you found any difficulties please let me know in the comment section below. I will reply you as soon as possible.
Reference: The MongoDB Database Tools Documentation
You may also like to read my other blog MongoDB useful commands.