Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB find() output to file

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
like image 625
Zac Avatar asked Dec 24 '22 12:12

Zac


1 Answers

You may try this:

mongo --quiet dbname  --eval 'printjson(db.collection.find().toArray())' > output.json
like image 106
Bùi Đức Khánh Avatar answered Jan 06 '23 12:01

Bùi Đức Khánh