In this tutorial, I will explain how you can export all collections in MongoDB.
Exporting is a facility that most databases and language frameworks support. This is done to make the data usable by other programs, applications, or languages in various forms. The common file types for database files are CSV, BSON, and JSON.
MongoDB allows developers to achieve this with the help of the mongoexport tool. The mongoexport tool must be executed outside the mongo shell as it cannot work there. This is a system command-line tool.
This tutorial will throw light on how we can export all collections in MongoDB to CSV (Comma Separated Value) and JSON file types. Let us first learn in brief about the mongoexport tool.
What is the mongoexport CLI Tool in MongoDB?
The mongoexport tool helps in exporting data from a MongoDB instance into a JSON or CSV file type. Since the MongoDB 4.4 launch, mongoexport tool is being offered separately from the MongoDB Server. It hence uses its own versioning. The version that is of our interest today, is 100.5.0.
The first released version of the mongoexport tool used to export all collections in MongoDB was 100.0.0. It was then released together with the MongoDB Server and adopted similar versioning.
The mongoexport CLI tool version 100.5.0 supports the below versions of MongoDB:
- MongoDB 5.0
- MongoDB 4.4
- MongoDB 4.2
- MongoDB 4.0
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 from the official MongoDB website.
- Visit the official downloads page of MongoDB.
- Under the heading “Choose which type of deployment is best for you”, click on the Tools button.
- Scroll down until you see the MongoDB Database Tools accordion and click to pull it down.
- Under the Available Downloads form, you will find the latest version of the database tools package selected by default. Select the platform of your system and the type of package you want.
- Let the installation process complete after which you can proceed to using the mongoexport tool.
How to Export Collection to JSON in MongoDB?
Let us get started with using the mongoexport tool to export all collections in MongoDB. To export collection to a JSON formatted file, follow the steps below.
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. We will use these details to demonstrate an example.
Syntax
mongoexport –db database_name –collection collection_name –out path_or_filename.json
Things to 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 it its own at that destination with that file name.
- mongoexport will then create and add data in the file automatically too.
Demonstration with an example
Let me illustrate an example so you can easily follow along with the steps to export all collections in MongoDB.
- Start your MongoDB server.
- Now, open your system terminal. Make sure you are not working the mongo shell.
- To export all collections in MongoDB in JSON format from a database, pass this 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?
A CSV file is usually handled by Microsoft Excel. We will make use of the same database, roxannesNursery. I will now show you how to export all documents in a collection to CSV, by working on the planter collection. To export collection to CSV in MongoDB, follow the steps mentioned below.
- Start your MongoDB server.
- Now, open your system terminal. Make sure you are not working the mongo shell.
- To export collection to CSV in MongoDB from a database, pass this command:
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: Caching a MongoDB Database with Redis
Conclusion
Exporting is a facility that most databases and language frameworks support. This is done to make the data usable by other programs, applications, or languages in various forms.
MongoDB allows developers to achieve this with the help of the mongoexport tool. This tutorial throws light on how we can export all collections in MongoDB to CSV (Comma Separated Value) and JSON file types