Quick Guide to Export All Collections in MongoDB

In MongoDB, we can export data to a variety of files which provides a convenience for the external programs to interact with it. Common file types in which data can be exported are TSV, CSV, BSON, and JSON.

MongoDB allows developers to achieve this export feature with the help of mongoexport tool. This tool must be executed outside the Mongo Shell as it may not work there. It is a System Command-Line Tool. Normally, this tool resides in the bin folder of MongoDB. However, if you don’t find it there, you can follow this article for step-by-step instructions on installation and setup. We will also learn to export MongoDB collection data to CSV and JSON file types. Let’s get started with the basic understanding of this tool.

Also Read: Find Records Where Array Field is Not Empty in MongoDB

What is the mongoexport CLI Tool in MongoDB?

The mongoexport tool helps in exporting data from a MongoDB database to a JSON, TSV, or CSV file type. Since the MongoDB 4.4 launch, mongoexport tool offered separately from the MongoDB Server. It hence uses its own versioning. The mongoexport version that we will use in this article is 100.9.3.

The mongoexport CLI tool version 100.9.3 supports the below versions of MongoDB:

  • MongoDB 7.0
  • MongoDB 6.0
  • MongoDB 5.0
  • MongoDB 4.4
  • MongoDB 4.2

Let us now proceed with installing the mongoexport tool. The mongoexport tool is offered with the MongoDB database tools package.

Installing mongoexport CLI Tool

Follow the steps below to install mongoexport tool:

1. Go to “https://www.mongodb.com/try/download/database-tools” where you will find lots of MongoDB tools for different purposes. The one we want is “MongoDB Command Line Database Tools Download”.

MongoDB Command Line Database Tools Download

2. Choose the version, platform, and package. We recommend you go with the latest version, then choose the OS in which you want to use the tool, just make sure to choose the correct architecture, then choose zip for downloading, it is much easier to go with. Finally, click on the download button.

3. After the file is downloaded, extract it and open the bin folder where you get different .exe files containing tools. 

4. Copy all of the .exe files and paste them into the “C:\Program Files\MongoDB\Server\4.4\bin” folder. This is the path where MongoDB is installed.

After all of the above steps, you can now able to use the tools you just copied and pasted. You may notice a tool named “mongoexport.exe”, this is the mongoexport tool we will be using for export.

How to Export Collection to JSON in MongoDB?

JSON stands for JavaScript Object Notation, it is a widely used format for storing data in the form of key-value pairs, similar to an object in JavaScript. It is language-independent making it easier to exchange data throughout various programming languages.

Let us get started with using the mongoexport tool to export MongoDB collections to a JSON file. 

Syntax:

mongoexport --db database_name --collection collection_name --out path_or_filename.json

Here you have to specify a file location where to export the data, a collection name and a database name from which you want to export.

Note:

  • The “–out” flag that we add here, stands for the directory path with the file name attached in the end.
  • You may choose to create a file with that name at that destination for mongoexport to use. or, you may leave it to mongoexport to create one on its own at that destination with that file name.
  • mongoexport will then create and add data in the file automatically too.

Additional Information:

Here we have not specified any format because the default format is JSON which is what we want, but you can use the “–type” option to specify other formats like CSV or TSV.

Example of Exporting Collections to JSON

Let’s illustrate an example for better understanding. Let us say we have a database named “roxannesNursery”. Inside this database, we have a set of collections that we would like to export, by the name “flowers”, “plants”, “seeds”, and “planters”. Let us also assume we have recorded a few documents in them. Now, learn how to export these collections.

Follow the steps below to export data from all collections to JSON:

1. Start up the MongoDB server by using the below command.

mongod

By default, the MongoDB instance runs on port 27017.

2. Open another command prompt tab and provide the absolute path of the “bin” folder of MongoDB where MongoDB tools are present. 

cd C:\Program Files\MongoDB\Server\4.4\bin

This is so that we can now use the tools located in the “bin” folder. This step ensures that if MongoDB tools are not in your system PATH environment variable still the “mongoexport” will run.

3. To export all collections in MongoDB in JSON format, run the following command. 

mongoexport --db roxannesNursery --collection flowers plants seeds planters --out C:\Users\Random\nurseryitemsdata.json

You will now see a JSON file created at the destination with the data in it.

How to Export a Collection to CSV in MongoDB?

Let’s see how to export a collection to CSV. The steps for exporting the collection to CSV are quite similar to those for exporting to JSON. We only need to pass an additional parameter flag “–type=csv”.

Example of Exporting a Collection to CSV

We will make use of the same database, “roxannesNursery”. Let’s see how to export all documents from a collection to CSV, by working on the “planter” collection. 

Below is the command to export a collection to CSV from a MongoDB database:

mongoexport --db roxannesNursery --collection planters --type=csv --fields name,size,price --out C:\Users\Random\nurseryplantersdata.csv

You should now be able to see a CSV file created at the specified destination with the required data inside it.

Read More: MongoDB Cursor in NodeJS

Conclusion

In this article, we learned how to export data in MongoDB using the mongoexport CLI tool. The steps we saw are very simple, you just have to install mongoexport tool, run the MongoDB server, and then execute the “mongoexport –db database_name –collection collection_name –out path_or_filename.json” command to export to JSON file. 

Similarly for exporting to a CSV file, execute the “mongoexport –db database_name –collection collection_name –type=csv –out path_or_filename.csv” command. Hope you enjoyed reading the content.

Reference

https://stackoverflow.com/questions/11255630/how-to-export-all-collections-in-mongodb

Aneesha S
Aneesha S
Articles: 172