How to Export All Collections in MongoDB

Tahseen Tauseef Feb 02, 2024
  1. mongoexport CLI Tool in MongoDB
  2. Install mongoexport CLI Tool
  3. Export Collection to JSON in MongoDB
  4. Export a Collection to CSV in MongoDB
How to Export All Collections 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:

  1. mongoexport CLI Tool in MongoDB
  2. Install the mongoexport CLI Tool
  3. Export Collection to JSON in MongoDB
  4. 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.

  1. MongoDB 5.0
  2. MongoDB 4.4
  3. MongoDB 4.2
  4. 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 Tools tab under the heading Choose which type of deployment is best for you.
  • Pull down the MongoDB Database Tools accordion by scrolling down and clicking it.
  • Under the Available Download form, 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 mongoexport utility.

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
  1. The out flag we add here stands for the directory path with the file name attached at the end.
  2. You may choose to create a file with that name for mongoexport at that destination. Or leave it to mongoexport to 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.

  1. Start your MongoDB server.
  2. Start by going to your system terminal. Check to see if you’re not using the mongo shell.
  3. 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 mongo shell.
  • 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.