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.
Prerequisite
Import and export in JSON
The two commands for JSON are:
mongoimport
andmongoexport
mongoimport
The command mongoimport
helps to import JSON file into the MongoDB database.
Syntax:
mongoimport --uri="MongoDB Cluster URI" --drop=filename.json
mongoexport
The mongoexport
command exports the data in the given formats.
Syntax:
mongoexport --uri="MongoDB Cluster URI" --collection=your_collection_name --out=my_collection_name.json
Example:
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 employees.json
file.
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.
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 --forceTableScan
parameters.
Import and export in BSON
The two commands for BSON are:
mongorestore
andmongo
dump
mongorestore
The command mongorestore
helps to import BSON data into MongoDb
database.
Syntax:
mongorestore --uri "Mongodb Cluster URI" --drop
Example:
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 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.
mongodump
Exports data in BSON.
Syntax:
mongodump --uri "MongoDB Cluster URI"
Example:
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.
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.