51

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.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
QVSJ
  • 1,165
  • 2
  • 12
  • 22

8 Answers8

84

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

peshkira
  • 6,069
  • 1
  • 33
  • 46
  • have you copy pasted this? if yes, please double check the db name and the collection name in the query. If you have just typed it yourself, double check your spelling. The error you are getting means that you have misspelled something... works for me (just tested it again) – peshkira Oct 10 '12 at 16:45
  • use your-moments your-moments --eval "var c = db.profile.find(); while(c.hasNext()) {printjson(c.next())}" >> "C:\exp\test.txt" where your-moments is the db name and profile is the collection name. – QVSJ Oct 10 '12 at 16:53
  • 1
    Thanks for the help. Will play around that and see if i get it to work. – QVSJ Oct 10 '12 at 16:59
  • 1
    try also this: `mongoexport --db your-moments --collection profile --out test.txt` – peshkira Oct 10 '12 at 17:04
  • It prints only [object object] in file – Shashank Nov 18 '13 at 09:30
  • this answer is one year old. Maybe something changed in the version you are using. You can try to use the printjson function on a simple object from your db in the mongo console to check if it still works. If not you will have to change the printjson function – peshkira Nov 19 '13 at 13:04
  • 1
    How do I do it for a remote mongo db? I tried `mongo blah.mongolab.com:33478/blah -u user -p pass --eval "my query" >> dump.txt` but it gave me `JavaScript execution failed: SyntaxError: Unexpected token ILLEGAL`. – Sheharyar Dec 05 '13 at 06:38
  • @SheharyarNaseer well, the error message is rather clear. it seems you have some problem with your javascript command, or maybe the enclosing quotes. If your query is longer, I'd double check it and store it in a file and load the whole file. – peshkira Dec 05 '13 at 08:45
  • 3
    This is correct, and I used this answer at first, but ran into a problem when I was doing a $not query and sending quoted fields into the query. Without the $not I could escape the quotes but with the $not I wasn't able to get it to work. In that case single quotes rather than double quotes around the entire eval may work better. – Paul Sep 11 '14 at 12:54
  • 1
    Worked great! Just remember to user single quotes or scape any double quote in your query. – Crysfel Aug 19 '19 at 16:17
28

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.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
8

Use this

mongo db_name --username user --password password < query1.js >> result.txt
Piotr Fryga
  • 506
  • 5
  • 8
3

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
Farzad Vertigo
  • 2,458
  • 1
  • 29
  • 32
amic
  • 85
  • 7
1

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+.

zevij
  • 2,416
  • 1
  • 23
  • 32
  • I think that the second gone should the command key "db", not ; like this: mongo : --eval "var x = db..; while(x.hasNext()) { printjson( x.next() ) }" >> out.txt – Loebre Jan 07 '19 at 13:52
1

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

Ken
  • 1,234
  • 10
  • 16
0
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
Derlin
  • 9,572
  • 2
  • 32
  • 53
DarwinFernandez
  • 344
  • 2
  • 6
-1

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

  • 1
    Including some explanation of your answer will help others that come across this solution. – Steven Aug 22 '16 at 19:06