MongoDB shell 中默认的漂亮打印
有没有办法告诉 Mongo 漂亮的打印输出?目前,所有内容都输出到一行,并且很难阅读,尤其是嵌套数组和文档。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
(注意:这是问题原始版本的答案,没有“默认”的要求)
您可以要求它很漂亮。
(note: this is answer to original version of the question, which did not have requirements for "default")
You can ask it to be pretty.
您可以添加
到
$HOME/.mongorc.js
中的文件,以默认全局启用漂亮打印。You can add
to your file in
$HOME/.mongorc.js
to enable pretty print globally by default.(注意:这是更新问题的答案)
您可以在 CLI 上执行此操作:
并且它总是会输出漂亮的结果。
(note: this is answer to the updated question)
You can just do this on the CLI:
And it's always going to output pretty results.
由于它基本上是一个 javascript shell,因此您还可以使用
toArray()
:但是,这将打印集合的所有文档,这与
pretty()
不同,它允许您迭代。请参阅:http://docs.mongodb.org/manual/reference/method/cursor.toArray/< /a>
Since it is basically a javascript shell, you can also use
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/
哦,所以我猜 .pretty() 等于:
Oh so i guess .pretty() is equal to:
尝试一下 Mongo-hacker(节点模块),它总是打印得很漂亮。
https://github.com/TylerBrock/mongo-hacker
更多它增强了 mongo shell(仅支持 ver>2.4,当前ver 为 3.0),例如
我在生产环境中使用的
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
I am using for while in production env, no problems yet.
回答了这个问题,但无法弄清楚如何从外部加载的 mongo 打印它。所以:
这适用于控制台:并且在控制台中是首选,但不适用于外部mongo加载的javascript:
这适用于外部mongo加载的javscript:
Got to the question but could not figure out how to print it from externally-loaded mongo. So:
This works is for console: and is prefered in console, but does not work in external mongo-loaded javascript:
This works in external mongo-loaded javscript:
看看这个:
Check this out: