How to export MongoDB data into CSV file? coding coffee

How to export MongoDB data into CSV file?

We may need to export MongoDB data into CSV from local server or remote server. Hence, in this post we can learn how to do it.

export MongoDb data into CSV
How to export MongoDB data into CSV file? 3

Export MongoDB data into CSV from local server with given fields with header name:

mongoexport --db my_db_name --collection my_collection_name --type=csv --fields field1,field2,field3 --out my_exported_csv_file_name.csv

If you want to export without header then use –noHeaderLine:

Example:

mongoexport --db my_db_name --collection my_collection_name --type=csv --fields field1,field2,field3 --out my_exported_csv_file_name.csv --noHeaderLine

If you want to export from remote server you need to install mongo-tools package using following command:

sudo apt install mongo-tools

And use the following script

mongoexport --host=myhost.com --port=27017 --db=my_db_name --collection=my_collection_name --type=csv --fields=field1,field2,field3 --out my_exported_csv_file_name.csv --noHeaderLine

Note: If you get unrecognized field ‘snapshot’ during the mongoexport. It may be due to missmatch version of mongoexport tool and mongo db server. In this case, we have to include –forceTableScan parameters.

mongoexport --host=myhost.com --port=27017 --db=my_db_name --collection=my_collection_name --type=csv --fields=field1,field2,field3 --out my_exported_csv_file_name.csv --noHeaderLine --forceTableScan


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments