How to get mongo command results in to a flat file

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)