Take the 2-minute tour ×

How do you export all the records in a mongodb collection to a csv ?


This asks me to specify name of the fields I need to export. Can I just export all the fields without specifying the names of fields?

share|improve this question

5 Answers 5

mongoexport  --help
-f [ --fields ] arg     comma separated list of field names e.g. -f name,age
--fieldFile arg         file with fields names - 1 per line

You have to manually specify it and if you think about it, it makes perfect sense. MongoDB is schemaless, CVS, on the other hand, has a fixed layout for columns. Without knowing what fields are used in different documents it's impossible to output the CVS dump.

If you have a fixed schema perhaps you could retrieve one document, harvest the field names from it with a script and pass it to mongoexport.

share|improve this answer
I was just searching if I could get the list of fields from the a record. i.e from db.collection.finOne().getFields(). But I guess that is not the right method(getFields). I tried getKeys() as well. Otherwise I will have to get the record with key:value hashes. –  Succeed Stha Jul 25 '11 at 9:56
I'm trying to do the same thing, but to figure out why it isn't import csv files properly. In my case, I need it to tell me everything about itself, including which fields it "invented" by itself. So in my case it doesn't make perfect sense to have to specify the fields, because I don't know what they all are! –  Stephen Oct 31 '11 at 18:46
On the field harvesting script front, I posted this a year or so back, may give you a few ideas. –  arober11 Sep 18 '13 at 16:07

@karoly-horvath has it right. Fields are required for csv.

According to this bug in the MongoDB issue tracker https://jira.mongodb.org/browse/SERVER-4224 you MUST provide the fields when exporting to a csv. The docs are not clear on it. That is the reason for the error.

Try this:

mongoexport --host localhost --db dbname --collection name --csv --out text.csv --fields firstName,middleName,lastName 
share|improve this answer

Also, you are not allowed spaces between comma separated field names.

BAD: -f firstname, lastname

GOOD: -f firstname,lastname

share|improve this answer
Thanks for that, I would have never realised it! –  Plasty Grove Feb 13 '13 at 8:59
+1 I was wondering why I was getting an error! Thanks –  Alexander Suraphel Jul 2 at 14:40

If you want, you can export all collections to csv without specifying --fields (will export all fields).

From http://drzon.net/export-mongodb-collections-to-csv-without-specifying-fields/ run this bash script


# fill in your details here

# first get all collections in the database
collections=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();db.getCollectionNames();"`;
collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();"`;

# for each collection
for ((i=0; i<${#collectionArray[@]}; ++i));
    echo 'exporting collection' ${collectionArray[$i]}
    # get comma separated list of keys. do this by peeking into the first document in the collection and get his set of keys
    keys=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys;" --quiet`;
    # now use mongoexport with the set of keys to export the collection to csv
    mongoexport --host $host -u $user -p $pass -d $dbname -c ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv;

share|improve this answer
Only issue with the above script is that it assumes the first document in each collection contains all possible keys that could appear in a document in that collection; which may not be the case if the document type can contain an array or nested sub document. –  arober11 Feb 17 at 19:52
@arober11 you're right, forgot to mention this important fact. What I usually do, is run a map reduce script to collect all keys periodically and use it to pull all the keys –  Michael Feb 19 at 15:30

You can upload the mongodb collection to:


and it will provide a good conversion to CSV without asking for any field names.

share|improve this answer
This is for JSON , not BSON format. –  Anil Dec 1 at 22:31

Your Answer


