Open In App

Export data from MongoDB

MongoDB allows you to export its data into other file formats like JSON or CSV so that the data can be used externally by other applications. So, to export data from the MongoDB database, MongoDB provides a command-line tool known as mongoexport. Using this tool you can exports data of a collection in JSON or CSV(comma-separated value) format. Moreover, we can also use features like limit and sort on a collection while exporting the data.

Note: mongoexport does not run in the mongo shell. So, when you use mongoexport tool you have to exit from the mongo shell.



Syntax:

For exporting data in JSON format



mongoexport –db database_name –collection collection_name –out path_or_name_of_the_file

For exporting data in CSV format 

mongoexport –db database_name –collection collection_name –type=csv –fields fields_names –out path_or_name_of_the_file

Important Points: 

Option

Description

–help It will return the information about option and the use of mongoexport 
–version  It will return the version of mongoexport
–db It will specify the name of the database in which mongoexport will run.
–collection It will specify the collection to export.
–fields It will specify the fields that will include in the export. If you are specifying multiple fields the use comma in between them. E.g. –fields name, class
–type It will specify the file type to export. For JSON format use json and for CSV format use csv.
–out It will specify a file in which the data is going to store.
–skip Using this option you can control from where mongoexport starts exporting files.  
–sort Using this option you can sort the exporting data. 
–limit Using this option you can limit the number of documents to export. 

Examples:

In the following examples, we are working with:

Database: GeeksforGeeks

Collection: students

Documents: five documents that contain the details of the students in the form of field-value pairs.

In this example, we are going to export all the documents present in the student collection to a JSON file(named as exportstudents.json) using mongoexport tool.

mongoexport –db GeeksForGeeks –collection students –out C:\Users\Darksider\exportstudents.json 

So, here we export all the 5 documents present in the student collection to a JSON file.

In this example, we are going to export only 2 documents from the student collection to a JSON file(named as studentslim.json) using mongoexport tool.

mongoexport –db GeeksForGeeks –collection students –limit 2 –out C:\Users\Darksider\studentslim.json

So, here we only export 2 documents from the student collection by setting up the value of –limit to 2.

In this example, we are going to export all the documents present in the student collection to a CSV file(named as students.csv) using mongoexport tool.

mongoexport –db GeeksForGeeks –collection students –type=csv –fields name,age –out C:\Users\Darksider\students.csv

Article Tags :