MongoDB find() output to file - mongodb

Is there a way to write the output of a MongoDB find() query to a file just by simply using a Linux shell command or running a script?
Right now I have to manually type in step-by-step. Example:
$ mongo
> use owndb
> db.CollectionName.find(<query>) ### and then copy and paste the result on a text editor

You may try this:
mongo --quiet dbname --eval 'printjson(db.collection.find().toArray())' > output.json

You can use mongoexport for that.
Example:
mongoexport -d dbname -c collection --jsonArray --pretty --quiet --out output.json

Related

Syntax missing ; before statement in mongoexport

I am trying to export mongodb collection with:
mongoexport -d InventoryPLC -c Spectra -o spectra.json;
but it is giving me error:
Syntax missing ; before statement
I search on mongodb website and other reference websites:
mongoexport -d InventoryPLC -c Spectra;
I want to export collection.
I checked stackoverflow examples and mongodb documentation:
mongoexport -d InventoryPLC -c Spectra -o spectra.json;
I want exported collection, but have had no luck.
Appreciate the help.
You are probably trying to run mongoexport from inside the Mongo shell. That command must be executed outside it, from your OS command shell.

How to export all collections in MongoDB

Can someone please tell me how to export all collections (into JSON Format) at a time through Command line
Steve's script generates the files with quotes at the ends. For this to not happen, the pattern is improved.
Improving the above script
#!/bin/bash
DB=$1
Collections=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | sed 's/[^"[:alnum:]]/ /g')
for collection in $Collections; do
mongoexport -d $DB -c $collection -o ./$collection.json
done
Generally you want to use mongoexport or mongodump but each of those only works on a single collection.
To get all collections and export them all you need to run some type of script that can locate and then loop through everything.
On linux it would be something like this:
#!/bin/bash
DB=$1
Collections=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | sed 's/,/ /g')
for collection in $Collections; do
mongoexport -d <DATABASE_NAME> -c $collection -o $collection.json
done
You'll need to this into a script file and then run it.
I haven't tested the above myself but it is hopefully error free.

export a csv from mongodb

I have two collections in my mongodb namely
1.companies
2.contacts
Both the companies and contact collection are interlinked. I want to export a particular companies contact into a csv. I have tried a mongo export command as follows
mongoexport --csv -d dbname -c contacts
-q {"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")};
-f {"first_name","last_name","title"} -o export.csv
I get a error as follows
SyntaxError: missing ; before statement (shell):1.
Please help me. Thanks in Advance
There could be a couple of things going on here. First, are you running mongoexport from the command line or from the mongo shell? The mongoexport command is run from the command line.
Secondly, you need to properly format the query and field parameters. You could enclose the query with single quotes, and the filed name is not a JSON document, but just a list of fields.
This would look like the following from the command line:
mongoexport --csv -d dbname -c contacts -q '{"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}' -f "first_name","last_name","title" -o export.csv
The following query will work if it is running from commandLine
mongoexport -h host -d dbname -c contacts --csv -q '{"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}' -f first_name,last_name,title -o export.csv

How to get mongo command results in to a flat file

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.
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
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.
Use this
mongo db_name --username user --password password < query1.js >> result.txt
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
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+.
Install MongoDB Compass, then it will have a tool to export query result to Json/CSV files.
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
mongoexport --db db_name --collection collection_name --csv --out file_name.csv -f field1,field2, field3

What does "too many positional options" mean when doing a mongoexport?

mongoexport -h db.mysite.com -u myUser -p myPass -c myCollection
But the response I get is:
ERROR: too many positional options
What's that about?
I had this same problem. In my case, I was using mongoexport with the --query option, which expects a JSON document, such as:
mongoexport ... --query {field: 'value'} ...
I needed to surround the document with quotes:
mongoexport ... --query "{field: 'value'}" ...
I had the same problem. Found a group post somewhere which said to remove the space between the '-p' and the password, which worked for me.
Your sample command should be:
mongoexport -h db.mysite.com -u myUser -pmyPass -c myCollection
The same error I have encountered while importing a csv file.
But its just, the fact that the field list which you pass for that csv file import may have blank spaces.
Just clear the blank spaces in field list.
Its the parsing error.
I had the same issue with mongodump. After searching a bit, I found out that using the --out parameter to specify the output directory would solve this issue. The syntax for using the out parameter is
mongoexport --collection collection --out collection.json
Also in case your Mongodb instance isn't running, then you could use the --dbpath to specify the exact path to the files of your instance.
Source: http://docs.mongodb.org/manual/core/import-export/
I had the same issue with the mongoexport utility (using 2.0.2). My fix was to use the FULL parameter name (i.e. not -d, instead use --db).
Sometimes editor will screw it up (such as evernote). I fixed the issue by retyping the command in terminal.
I was also stuck in same situation and found what was causing it.
Make sure you are exporting in CSV format by adding parameter --type csv
Make sure there are no spaces in fields name,
Example: --fields _id, desc is wrong but --fields id,desc,price is good
This also works if you place the -c option first. For me, this order does work:
mongoexport -c collection -h ds111111.mlab.com:11111 -u user -p pass -d mydb
You can also leave the pass out and the server will ask you to enter the pass. This only works if the server supports SASL authentication (mlab does not for example).
for the (Error: Too many arguments)
Dont Use Space Between the Fields
try:
mongoexport --host localhost --db local --collection epfo_input --type=csv --out epfo_input.csv --fields cin,name,search_string,EstablishmentID,EstablishmentName,Address,officeName
Dont_Try:
mongoexport --host localhost --db local --collection epfo_input --type=csv --out epfo_input.csv --fields cin,name,search_string,Establishment ID,Establishment Name,Address,office Name
Had a similar issue
$too many positional arguments
$try 'mongorestore --help' for more information
Simply fix for me was to wrap the path location in quotes " "
This Failed:
mongorestore -h MY.mlab.com:MYPORT -d MYDBNAME -u ADMIN -p PASSWORD C:\Here\There\And\Back\Again
This Worked:
mongorestore -h MY.mlab.com:MYPORT -d MYDBNAME -u ADMIN -p PASSWORD "C:\Here\There\And\Back\Again"
I had the same issue with starting mongod. I used the following command:
./mongod --port 27001 --replSet abc -- dbpath /Users/seanfoley/Downloads/mongodb-osx-x86_64-3.4.3/bin/1 --logpath /Users/seanfoley/Downloads/mongodb-osx-x86_64-3.4.3/log.1 --logappend --oplogSize 5 --smallfiles --fork
The following error message appeared:
Error parsing command line: too many positional options have been specified on the command line
What fixed this issue was removing the single space between the '--' and 'dbpath'
I had the same issue while using the "mongod --dbpath" command. What I was doing looked somewhat like this:
mongod --dbpath c:/Users/HP/Desktop/Mongo_Data
where as the command syntax was supposed to be:
mongod --dbpath=c:/Users/HP/Desktop/Mongo_Data
This worked for me. Apart from this one may take a note of the command function and syntaxes using the mongod --help command.
In my case, I had to write the port separately from the server connection. This worked for me:
mongoexport --host=HOST --port=PORT --db=DB --collection=COLLECTION
--out=OUTPUT.json -u USER -p PASS
Create a json file in the same folder where you have your mongod.exe.
eg: coll.json
and open a command prompt in this folder.
type this below in CMD.
mongoexport --db databasename --collection collectionname --out
coll.json
and you will see like a progress bar very cool exporting all data.