Pretty print in MongoDB shell as default

raffian Source

Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.

mongodbcommand-line-interfacemongo-shell

Answers

answered 6 years ago Sergio Tulentsev #1

(note: this is answer to original version of the question, which did not have requirements for "default")

You can ask it to be pretty.

db.collection.find().pretty()

answered 6 years ago staackuser2 #2

You can add

DBQuery.prototype._prettyShell = true

to your file in $HOME/.mongorc.js to enable pretty print globally by default.

answered 5 years ago Aafreen Sheikh #3

Since it is basically a javascript shell, you can also use toArray():

db.collection.find().toArray()

However, this will print all the documents of the collection unlike pretty() that will allow you to iterate. Refer: http://docs.mongodb.org/manual/reference/method/cursor.toArray/

answered 5 years ago Goff #4

Oh so i guess .pretty() is equal to:

db.collection.find().forEach(printjson);

answered 4 years ago Bhanu Chawla #5

(note: this is answer to the updated question)

You can just do this on the CLI:

echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js

And it's always going to output pretty results.

answered 3 years ago Gaurav Gandhi #6

Give a try to Mongo-hacker(node module), it alway prints pretty. https://github.com/TylerBrock/mongo-hacker

More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like

  • Colorization
  • Additional shell commands (count documents/count docs/etc)
  • API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
  • Aggregation Framework

I am using for while in production env, no problems yet.

answered 2 years ago Mohammad Heydari #7

db.collection.find().pretty()

above command will make your result like this :Using Pretty command

the stu is the name of my collection , you should use your own collection name instead it. i hope the screenshot help you in a good way.

comments powered by Disqus