In Mongo, how do I pretty-print results so .find() looks like .findOne()

findOne() results in pretty-print json object.

find() results in jarbled json object.

How can I make find() the same as findOne(), when it comes to display in the mongo shell?


If you are scripting using javascript, you can use dcrosta's answer. But if you want to pretty print directly on the mongo interactive shell, you have to append pretty() to your find() queries.

Type on the shell: db.yourcollection.find().pretty()


The cursor object returned by find() supports forEach(), so you can use:

db.foo.find().forEach(printjson)

However note that, unlike the default output of find() which shows the first 10 objects then lets you choose whether to continue iterating or not, forEach() will iterate the entire result set. Thus if your query returns many results, this may take a while and may not be terribly helpful. limit() is your friend here.