How to export collection to CSV in MongoDB?

前端 未结 11 1588
刺人心
刺人心 2020-12-12 11:52

How do you export all the records in a MongoDB collection to a .csv file?

mongoexport --host localhost --db dbname --collection name --type=csv          


        
11条回答
  •  情话喂你
    2020-12-12 12:01

    I could not get mongoexport to do this for me. I found that,to get an exhaustive list of all the fields, you need to loop through the entire collection once. Use this to generate the headers. Then loop through the collection again to populate these headers for each document.

    I've written a script to do just this. Converting MongoDB docs to csv irrespective of schema differences between individual documents.

    https://github.com/surya-shodan/mongoexportcsv

提交回复
热议问题