Updated March 14, 2023
Introduction to MongoDB Export to Excel
MongoDB export to excel is defined as the export of the collected data into the CSV file in MongoDB; we can export the specific field or all field data of collection into excel or CSV file. We can export the data by using the mongo export command. We can also export the collected data into the JSON or TSV file. Mongo export is the utility of the MongoDB server, which was located in the bin directory.
How to Export Data to Excels in MongoDB using Various Methods?
Below are the various methods of data export into excel available in MongoDB.
1. Export the whole Collection Data into the CSV or Excel File
- We can export the whole collection data into the CSV file by using the mongoexport command are as follows.
- Below is the example of exporting whole collection data into the CSV file using the mongoexport command.
Syntax:
- Mongoexport
- –host (host name of database server)
- -u (name of the user which was used to export the data into the file.)
- -p (password of the user which was used to export the data into the file.)
- –db (Name of database which was used to export the data into the file.)
- –collection (name of collection which was used to export the data into the file.)
- –out /name_of_file (Name of file which was we have used to export the data from collection to file.)
Example:
Below is the example of export the whole collection data into the CSV file in MongoDB.
Command:
mongoexport --db admin --collection import1 --out test.csv
cat test.csv
Output:
- In the above example, we have exported import1 collection data into the test.csv file by using the mongoexport command.
- We have used the database name as admin and collection name as import1.
- We have used the output file name as test.csv to export the data from the collection into the file.
2. Export the Specified querycollection Data into the CSV or Excel File
- We can export the specified querycollection data into the CSV file using the mongoexport command.
- Below is the example of export specified query collection data into the CSV file by using the mongoexport command.
Syntax:
- Mongoexport
- –host (host name of database server)
- -u (name of the user which was used to export the data into the file.)
- -p (password of the user which was used to export the data into the file.)
- –db (Name of database which was used to export the data into the file.)
- –collection (name of collection which was used to export the data into the file.)
- –query ‘{“field_name (field name which was we have used with mongoexport command)”: “document name”}.’
- –out /name_of_file (Name of file which was we have used to export the data from collection to file.)
Example:
Below is the example of export the specified query collection data into the CSV file in MongoDB.
Command:
mongoexport --db admin --collection import1 --query '{"item": "paper"}' --out test1.csv
cat test1.csv
Output:
- In the above example, we have exported import1 collection data into the test1.csv file by using the mongoexport command.
- We have used the database name as admin and collection name as import1.
- We have used the output file name as test1.csv to export the data from the collection into the file.
- We have exported only which document which contains the name as paper.
3. Export the Specified fieldcollection data into the CSV or Excel File
- We can export the specified field collection data into the CSV file by using the mongoexport command are as follows.
- Below is the example of export specified field collection data into the CSV file by using the mongoexport command.
Syntax:
- Mongoexport
- –host (host name of database server)
- -u (name of the user which was used to export the data into the file.)
- -p (password of the user which was used to export the data into the file.)
- –db (Name of database which was used to export the data into the file.)
- –collection (name of collection which was used to export the data into the file.)
- –fields (field name used to export the data into CSV file.)
- –out /name_of_file (Name of file which was we have used to export the data from collection to file.)
Example:
Below is the example of export the specified field collection data into the CSV file in MongoDB.
Command:
mongoexport --db admin --collection import1 --fields _id,item,qty,size --out test2.csv
cat test2.csv
Output:
4. Export the collected data into the CSV or Excel file using Limit
- We can export the collected data into the CSV file by using the mongoexport command are as follows.
- Below is the example of export collection data into the CSV file by using limit.
Syntax:
- Mongoexport
- –host (host name of database server)
- -u (name of the user which was used to export the data into the file.)
- -p (password of the user which was used to export the data into the file.)
- –db (Name of database which was used to export the data into the file.)
- –collection (name of collection which was used to export the data into the file.)
- –limit (Number of documents which was we have exporting into the file.)
- –out /name_of_file (Name of file which was we have used to export the data from collection to file.)
Example:
Below is the example of export the whole collection data into the CSV file.
Command:
mongoexport --db admin --collection import1 --limit 4 --out test_exp.csv
cattest_exp.csv
Output:
Recommended Article
This is a guide to MongoDB Export to Excel. Here we discuss Introduction and how to Export Data to Excel in MongoDB using Various Methods along with its different Commands and its examples. You can also go through our other suggested articles to learn more –