MongoDB

MongoDB Export All Collections

“When working with databases, you may encounter scenarios where you need to transfer your data from one source to another. Learning how to export your data, whether using it in a different application or moving it into a different cluster, is essential.

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.

$ wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian11-x86_64-100.6.0.deb

Install the package:

$ sudo dpkg -i mongodb-database-tools-debian11-x86_64-100.6.0.deb

This should install the MongoDB Database Tools suite, which includes the mongo export utility.

You can verify by running the command:

$ mongoexport --version

This should return details about the install mongexport utility as shown:

mongoexport version: 100.6.0

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.

mongoexport --collection=<target_collection>--db=<target_database>--out=filename.json

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:

bash-3.2$ mongoexport --collection=netflix --db=entertainment --out=netflix.json

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.388+0300 connected to: mongodb://localhost/

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:

> show collections

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:

mongodump -d entertainment -o entertainment

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:

bash-3.2$ ls

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:

$ mongoexport --collection=hulu --db=entertainment --type=csv --fields=show_id,type,title,date_added,release_year,rating,listen_in,description --out=hulu.csv

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.106+0300 connected to: mongodb://localhost/

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!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list