How to Export All Collections in MongoDB
-
mongoexportCLI Tool in MongoDB -
Install
mongoexportCLI Tool - Export Collection to JSON in MongoDB
- Export a Collection to CSV in MongoDB
This MongoDB tutorial will show you how to export all of your MongoDB collections.
Most databases and language frameworks allow you to export your data. This makes the data useful in various forms by other programs, applications, or languages.
CSV, BSON, and JSON are the most common database file formats.
Using the mongoexport tool, MongoDB allows developers to accomplish this because the mongoexport tool does not work in the mongo shell; it must be executed outside it. This is a system command-line tool.
This tutorial will highlight how you can export all collections in MongoDB to CSV (Comma Separated Value) and JSON file types. But let us first learn about the mongoexport tool.
This article demonstrates the following:
mongoexportCLI Tool in MongoDB- Install the
mongoexportCLI Tool - Export Collection to JSON in MongoDB
- Export a Collection to CSV in MongoDB
mongoexport CLI Tool in MongoDB
The mongoexport tool helps export data from a MongoDB instance into a JSON or CSV file type. Since the MongoDB 4.4 launch, the mongoexport tool has been offered separately from the MongoDB Server.
It uses its versioning. The mongoexport CLI tool supports the below versions of MongoDB.
- MongoDB 5.0
- MongoDB 4.4
- MongoDB 4.2
- MongoDB 4.0
Let’s get started with setting up the mongoexport tool. The MongoDB Database Tools package includes the mongoexport tool.
Install mongoexport CLI Tool
You will follow the steps below to install mongoexport from the official MongoDB website.
-
Visit MongoDB’s official downloads page.
-
Click the
Toolstab under the headingChoose which type of deployment is best for you. -
Pull down the
MongoDB Database Toolsaccordion by scrolling down and clicking it. -
Under the
Available Downloadform, the latest version of the database tools package is selected by default. Choose the platform for your system and the package you require. -
Allow the installation process to finish before utilizing the
mongoexportutility.
Export Collection to JSON in MongoDB
Let’s start by using the mongoexport tool to export all collections in MongoDB. Then, follow the instructions below to export the collection to a JSON file.
Let’s say we have a database named teams. We have a set of collections inside this database called PremierLeague, LaLiga, SerieA, and Ligue1 that we would like to export.
Let’s also assume you have recorded a few documents in them. You will use these details to demonstrate an example.
The syntax for this is given below.
mongoexport –db database_name –collection collection_name –out path_or_filename.json
- The
outflag we add here stands for the directory path with the file name attached at the end. - You may choose to create a file with that name for
mongoexportat that destination. Or leave it tomongoexportto create one on its own at that destination with that file name.
mongoexport will create and add data to the file automatically.
Let’s illustrate an example so you can easily follow along with the steps to export all collections in MongoDB.
- Start your MongoDB server.
- Start by going to your system terminal. Check to see if you’re not using the
mongoshell. - To export all collections in MongoDB in JSON format from a database, pass this command:
mongoexport –db teams –collection PremierLeague LaLiga SerieA Ligue1 –out C:\Users\Random\teamsdata.json
You should now see a JSON file containing the data created at the destination.
Export a Collection to CSV in MongoDB
Microsoft Excel usually handles a CSV file. You will use the same database, teams.
To export the collection to CSV in MongoDB, follow the steps mentioned below.
-
Start your MongoDB server.
-
Start by going to your system terminal. Check to see if you’re not using the
mongoshell. -
To export collection to CSV in MongoDB from a database, pass this command:
mongoexport –db teams –collection Ligue1 –type=csv –fields team,size,price –out C:\Users\Random\teamssdata.csv
The user should now be able to view a CSV file with the appropriate data created at the provided location.