Skip to content

Instantly share code, notes, and snippets.

@dinolupo
Last active November 13, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinolupo/4e87cf12970e95e10c04 to your computer and use it in GitHub Desktop.
Save dinolupo/4e87cf12970e95e10c04 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment