How to get mongo command results in to a flat file

后端 未结 8 2071
攒了一身酷
攒了一身酷 2021-01-30 06:26

How do I export the results of a MongoDB command to a flat file

For example, If I am to get db.collectionname.find() into a flat file.

I tried

相关标签:
8条回答
  • 2021-01-30 07:28

    you can try the following from the command line

    mongo 127.0.0.1/db --eval "var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt
    

    assuming you have a database called 'db' running on localhost and a collection called 'collection' this will export all records into a file called test.txt

    If you have a longer script that you want to execute you can also create a script.js file and just use

    mongo 127.0.0.1/db script.js >> test.txt
    

    I hope this helps

    0 讨论(0)
  • 2021-01-30 07:28

    mongoexport --db db_name --collection collection_name --csv --out file_name.csv -f field1,field2, field3

    0 讨论(0)
提交回复
热议问题