How to get mongo command results in to a flat file

MongodbMongodb JavaMongodb Query

Mongodb Problem Overview


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 db.collectionname.find() >> "test.txt" doesnt seem to work.

Mongodb Solutions


Solution 1 - Mongodb

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

Solution 2 - Mongodb

I know of no way to do that from the mongo shell directly, but you can get mongoexport to execute queries and send the results to a file with the -q and -o options:

mongoexport -h mongo.dev.priv -d models -c profiles -q '{ $query : { _id : "MRD461000" } }' -o MRD_Series1.json

The above hits queries the profiles collection in the models database grabbing the JSON document for _id = "MRD641000". Works for me.

Solution 3 - Mongodb

Use this

mongo db_name --username user --password password < query1.js >> result.txt

Solution 4 - Mongodb

Try this - returns a json file with the data of the query, you can change .json for .txt and other.

mongoexport --db products --collection clicks --query '{"createdInt":{$gte:20190101}, "clientId":"123", "country":"ES"}' --out clicks-2019.json

Solution 5 - Mongodb

Having missed the db needing to be the actual db in Peshkira's answer, here is a general syntax for a one liner in shell (assuming no password):

mongo <host>:<db name> --eval "var x = <db name>.<collection name>.<query>; while(x.hasNext()) { printjson( x.next() ) }" >> out.txt

I tested it both on my mac and Google cloud Ubuntu 15 with Mongo 3+.

Solution 6 - Mongodb

Install MongoDB Compass, then it will have a tool to export query result to Json/CSV files.

Solution 7 - Mongodb

mongoexport --host 127.0.0.1 --port 27017 --username youruser -p yourpass \
   -d yourDatabaseName -c collectionName --type csv \
   --fields field1,field2 -q '{"field1" : 1495730914381}' \
   --out report.csv

Solution 8 - Mongodb

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQVSJView Question on Stackoverflow
Solution 1 - MongodbpeshkiraView Answer on Stackoverflow
Solution 2 - MongodbBob KuharView Answer on Stackoverflow
Solution 3 - MongodbPiotr FrygaView Answer on Stackoverflow
Solution 4 - MongodbamicView Answer on Stackoverflow
Solution 5 - MongodbzevijView Answer on Stackoverflow
Solution 6 - MongodbKenView Answer on Stackoverflow
Solution 7 - MongodbDarwinFernandezView Answer on Stackoverflow
Solution 8 - MongodbArnab GhoshView Answer on Stackoverflow