Import and Export in MongoDB

Import and Export in MongoDB

In this post, we will learn the different types of import and export commands in MongoDB. Let’s say we have data in our MongoDB database and want to keep a backup of it by exporting or we want to insert data into the MongoDB database by importing from an external file. In MongoDB, both options are possible.

First, we have to decide what format of data we are going to use. MongoDB supports both JSON and BSON data formats. If you don’t know what is JSON and BSON I highly recommend reading the article difference between JSON and BSON.

BSON is great but it is not readable. If the use case is to export data from the server and view/read in the local machine then exporting into JSON is highly recommended. However, exporting from one server and importing to another server is the use case then exporting data to BSON is a great choice. BSON is lighter and faster than JSON.

There are four commands n MongoDB to import and export data. Two commands are for exporting and importing data in JSON format and the other two are for importing and exporting in BSON format.


Import and export in JSON

The two commands for JSON are:

  • mongoimport and
  • mongoexport


The command mongoimport helps to import JSON file into the MongoDB database.


mongoimport --uri="MongoDB Cluster URI" --drop=filename.json


The mongoexport command exports the data in the given formats.


mongoexport --uri="MongoDB Cluster URI" --collection=your_collection_name --out=my_collection_name.json


mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>" --collection=employees--out=employees.json

The above command exports employees collection data from the database defined in URI and saves data into employees.json file.

Similarly, we can easily export MongoDB data in CSV format as well. See an example below:

mongoexport --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.

–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.

Import and export in BSON

The two commands for BSON are:

  • mongorestore and
  • mongodump


The command mongorestore helps to import BSON data into MongoDb database.


mongorestore --uri "Mongodb Cluster URI" --drop


mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>" --drop

In this command, we’ve defined an option –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. This option helps us to avoid errors like duplicate data entry during mongorestore.


Exports data in BSON.


mongodump --uri "MongoDB Cluster URI"


mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>"

If the credentials are correct in the connection string of your URI then it should export a BSON file inside your_database_name folder.


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.

Notify of
Inline Feedbacks
View all comments