How to Import and Export Data in MongoDB: A Step-by-Step Guide

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!

Prerequisite

  1. Install MongoDB on your machine.
  2. Ensure MongoDB’s bin directory (e.g., C:\Program Files\MongoDB\Server\6.0\bin) is added to your system’s PATH.
  3. Have a running MongoDB instance (local or remote).
  4. 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.

OptionDescription
–hostHost name where the MongoDB server is. It can be any hostname like IP Address
–portIt defines the port number. The default port MongoDB uses is 27017
–dbThe database name
–collectionCollection name that you want to export
–typeThe type of the exported file.
–fieldsIt exports data in the exported files from the given fields only.
–outWe can define the output file name.
–noHeaderLineThis does not include the field name in the header of each column.
mongoexport command options

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 --forceTableScan parameters.

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

Featuremongoexport/mongoimportmongodump/mongorestore
Data FormatJSON/CSVBSON (binary)
Use CasePartial data transferFull database backup
SpeedSlowerFaster
Data FidelityMay lose BSON typesPreserves all data types

Pro Tips

  1. Authentication: Include credentials in the URI for remote databases:
    mongodb://username:password@host:port/database.
  2. Exclude Fields: Use --fields="field1,field2" to export specific fields.
  3. Compression: Add --gzip to reduce backup size (e.g., mongodump --gzip).

When to Use Which Tool?

  • Use mongoexport/mongoimport for:
    • Sharing CSV/JSON files.
    • Migrating specific collections.
  • Use mongodump/mongorestore for:
    • 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.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments