Pretty print in MongoDB shell as default

2019-01-15 23:36发布

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.

7条回答
Evening l夕情丶
2楼-- · 2019-01-16 00:07

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/

查看更多
别忘想泡老子
3楼-- · 2019-01-16 00:08

(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()
查看更多
做自己的国王
4楼-- · 2019-01-16 00:10

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.

查看更多
时光不老,我们不散
5楼-- · 2019-01-16 00:12

Check this out:

db.collection.find().pretty()
查看更多
看我几分像从前
6楼-- · 2019-01-16 00:14

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

db.collection.find().forEach(printjson);
查看更多
Ridiculous、
7楼-- · 2019-01-16 00:23

You can add

DBQuery.prototype._prettyShell = true

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

查看更多
登录 后发表回答