Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export mongoDB data into a CSV format?

Tags:

csv

excel

mongodb

I'm looking for a very simple way to export data from mongoDB into a CSV. Most of the answers involve bash scripts… etc. Is there a simple mongoDB command that will just export the data into CSV?

like image 880
DBWeinstein Avatar asked Nov 08 '13 15:11

DBWeinstein


2 Answers

Update:

As of mongo 3.0.6 --csv is no longer supported and the new flag is --type=csv, so the command would be

mongoexport --db users --collection contacts --type=csv --fieldFile fields.txt

Original answer:

This can be done from the command line using the mongo utility function mongoexport --csv.

Alongside the --csv the documentation states that you also need to use --fields or specify a file with the fields in using --fieldFile.

Have a look at the usage examples and see if they help, for example:

mongoexport --db users --collection contacts --csv --fieldFile fields.txt
like image 188
andyb Avatar answered Oct 14 '22 23:10

andyb


If you already have the .bson file, in order to export to csv:

bsondump collection.bson > file.csv
like image 39
Fabio Espinosa Avatar answered Oct 14 '22 22:10

Fabio Espinosa