Thankfully, MongoDB provides ways to export our data into various formats, such as JSON, CSV, and BSON. In this tutorial, we will focus on learning how to export MongoDB collections into the two most popular formats, CSV and JSON.”
Let us jump in.
Install the Mongoexport Utility
To export a single or multiple MongoDB collections, we use the mongoexport utility. This command line tool allows us to connect to the MongoDB cluster and export a specific collection to a specific format.
Before we can use this Mongo Export command, we need to ensure the tool is installed.
Open your browser and navigate to the link below:
https://www.mongodb.com/try/download/database-tools
Locate the installer for your machine and download the provided installer. You can then follow the instructions for setting up the database tools for your system.
If you are on Debian and Debian-based distributions, run the following commands to install the MongoDB database tools.
Install the package:
This should install the MongoDB Database Tools suite, which includes the mongo export utility.
You can verify by running the command:
This should return details about the install mongexport utility as shown:
git version: 1d46e6e7021f2f5668763dba624e34bb39208cb0
Go version: go1.17.10
os: darwin
arch: amd64
compiler: gc
MongoDB Export Collection Using Mongo Export Command
The following shows the command syntax you can use to export your collection into JSON format.
The -out parameter allows you to specify the path to the filename. You can leave this parameter, and MongoDB will generate the output file with the name of the specified collection.
Let us discuss how we can export our data from a given collection. The first step is to ensure that the MongoDB server is up and running.
Suppose we wish to export a collection under the name “netflix” in the “entertainment” database; we can run the command as shown:
The command above should export all the records from the netflix collection into the netflix.json file. The command will default save the resulting JSON file in the current working directory.
The command should return the number of exported documents as shown in the output below:
2022-09-23T19:06:54.564+0300 exported 8807 records
Export All Collections
Suppose we have multiple collections in a given database and wish to export all of them at once.
An example scenario is as shown:
amazon_prime
hulu
netflix
In the above example, we have three collections from the entertainment database. To export all of them, we can run the command:
In this case, we use the mongodump command to export all the collections of the entertainment database.
This should show output as:
The command will save the exported data into the entertainment directory. The resulting files are as shown:
amazon_prime.bson hulu.bson netflix.bson amazon_prime.metadata.json hulu.metadata.json netflix.metadata.json
Export Collection as CSV Format
To export a collection in CSV format, use the –type and –field parameters in the mongoexport command.
Suppose our document’s structure is as shown:
We export the data into CSV format as shown:
In the example, we need to specify the fields we wish to export from the collection as a comma-separated list.
The command should return output as:
2022-09-23T19:28:14.170+0300 exported 3073 records
In this case, the command exports 3073 records into the hulu.csv file. You can use this file to transfer the data to another cluster or pass it into a CSV parser.
Conclusion
This tutorial explores the various methods of exporting data from a MongoDB collection using the mongoexport command. You can check the command documentation to discover more.
Thanks for reading & Stay tuned for more!!