Pretty print in MongoDB shell as default

Solution 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()

Solution 2:

You can add

DBQuery.prototype._prettyShell = true

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

Solution 3:

(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.

Solution 4:

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/

Solution 5:

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

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