Import and Export in MongoDB

By | October 17, 2022

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

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.

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.

Import and export in BSON

The two commands for BSON are:

  • mongorestore and
  • mongodump

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.

Reference: The MongoDB Database Tools Documentation

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments