How to export collection to CSV in MongoDB?

前端 未结 11 1541
刺人心
刺人心 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:23

    For all those who are stuck with an error.

    Let me give you guys a solution with a brief explanation of the same:-

    command to connect:-

    mongoexport --host your_host --port your_port -u your_username -p your_password --db your_db --collection your_collection --type=csv --out file_name.csv --fields all_the_fields --authenticationDatabase admin
    

    --host --> host of Mongo server

    --port --> port of Mongo server

    -u --> username

    -p --> password

    --db --> db from which you want to export

    --collection --> collection you want to export

    --type --> type of export in my case CSV

    --out --> file name where you want to export

    --fields --> all the fields you want to export (don't give spaces in between two field name in between commas in case of CSV)

    --authenticationDatabase --> database where all your user information is stored

提交回复
热议问题