How to export your MongoDB collections to CSV files
For some reasons, we might want to export our data in our mongodb collections into csv files. Is it possible? Yes, it is. Mongodb provide a tools called mongoexport which can used to export your data to JSON or CSV files.
Here is the usage example.
mongoexport –db pricebook –collection product_specification –csv –fields “product_id,category_id,specs” -o specs.csv
This operation written above is exporting product_specification collection in pricebook database into specs.csv file with specified fields. If you want to export your data into CSV file you have to specify what fields that you want to exports. If you want to export into JSON file you don’t have to specifiy the fields.
Check the complete mongodb documentation of mongoexport on the link below. All you need to know about mongoexport are there, usage overview and its complete options.
Now, you know how to export your mongodb collections into csv files. What if you have 99 collections? What if in every collections you have more than 100 fields? Exporting it one by one and specifying fields for each collections is bothersome. Sometimes you might want to export all of your collections without specifying field name.
Thanks to drzon and his little bash script. Here is the script.
Hope that helps. Cheers.