Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Mongo query output to a file while in the mongo shell

2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with Mongo, while being in the shell

I can easily get the output of a query I want by being outside of the shell and executing the following command:

mongo localhost:27017/dbname --eval "printjson(db.collectionName.findOne())" > sample.json 

The above way is fine, but it requires me to exit the mongo shell or open a new terminal tab to execute this command. It would be very convenient if I could simply do this while still being inside the shell.

P.S: the Question is an offshoot of a question I posted on SO

like image 663
Parijat Kalia Avatar asked Mar 21 '14 17:03

Parijat Kalia


People also ask

How do I print a variable in mongo shell?

You can execute Java Script in Mongo CLI or command prompt The print() method simply prints the data arguments that are passed into it. print("Hello From Mongo"); eg: print(data, ...); printjson(object); print(JSON.

How do I export data from MongoDB to CSV?

Export MongoDB to CSV (e.g. Excel) Open the Export Wizard and select your export source. This screen only appears if you haven't chosen an item in the Connection Tree, run a previous query, or selected specific documents. Next, choose CSV as the export format then click Next.

How do I run a query in MongoDB terminal?

To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.


2 Answers

AFAIK, there is no a interactive option for output to file, there is a previous SO question related with this: Printing mongodb shell output to File

However, you can log all the shell session if you invoked the shell with tee command:

$ mongo | tee file.txt MongoDB shell version: 2.4.2 connecting to: test > printjson({this: 'is a test'}) { "this" : "is a test" } > printjson({this: 'is another test'}) { "this" : "is another test" } > exit bye 

Then you'll get a file with this content:

MongoDB shell version: 2.4.2 connecting to: test > printjson({this: 'is a test'}) { "this" : "is a test" } > printjson({this: 'is another test'}) { "this" : "is another test" } > exit bye 

To remove all the commands and keep only the json output, you can use a command similar to:

tail -n +3 file.txt | egrep -v "^>|^bye" > output.json 

Then you'll get:

{ "this" : "is a test" } { "this" : "is another test" } 
like image 195
Roberto Avatar answered Oct 07 '22 07:10

Roberto


We can do it this way -

mongo db_name --quiet --eval 'DBQuery.shellBatchSize = 2000; db.users.find({}).limit(2000).toArray()' > users.json 

The shellBatchSize argument is used to determine how many rows is the mongo client allowed to print. Its default value is 20.

like image 42
Jyotman Singh Avatar answered Oct 07 '22 07:10

Jyotman Singh