Zac
Zac

Reputation: 159

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

Upvotes: 5

Views: 14346

Answers (2)

You may try this:

mongo --quiet dbname  --eval 'printjson(db.collection.find().toArray())' > output.json

Upvotes: 7

Neodan
Neodan

Reputation: 5252

You can use mongoexport for that.

Example:

mongoexport -d dbname -c collection --jsonArray --pretty --quiet --out output.json

Upvotes: 6

Related Questions