dinolupo
11/10/2015 - 12:01 PM

Mongo Commands

Mongo Commands

Exporting a mongo query result to file "products.json"

mongo --quiet databaseName --eval "var c = db.products.find(); while(c.hasNext()) {printjson(c.next())}" > products.json

OR

create a Javascript file like the following:

vim query.js

use databaseName
db.products.find().pretty()

and execute from shell:

mongo < query.js > products.json

OR with heredoc from shell

mongo --quiet databaseName << EOF > products.json
db.products.find()
EOF