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 helps to import JSON file into the MongoDB database.
mongoimport --uri="MongoDB Cluster URI" --drop=filename.json
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>.mongodb.net/your_database_name" --collection=employees--out=employees.json
The above command exports
employees collection data from the database defined in URI and saves data into
Similarly, we can easily export MongoDB data in CSV format as well. See an example below:
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.
|–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
Import and export in BSON
The two commands for BSON are:
mongorestore helps to import BSON data into
mongorestore --uri "Mongodb Cluster URI" --drop
mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/your_database_name" --drop
In this command, we’ve defined an option –drop. If you have a database name
employees then it will drop the
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>.mongodb.net/your_database_name"
If the credentials are correct in the connection string of your URI then it should export a BSON file inside your_database_name folder.
Reference: The MongoDB Database Tools Documentation