MongoDB shell 中默认的漂亮打印

发布于 2025-01-02 03:34:15 字数 61 浏览 6 评论 0原文

有没有办法告诉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

演出会有结束 2025-01-09 03:34:15

注意:这是问题原始版本的答案,没有“默认”的要求

您可以要求它很漂亮。

db.collection.find().pretty()

(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()
就是爱搞怪 2025-01-09 03:34:15

您可以添加

DBQuery.prototype._prettyShell = true

$HOME/.mongorc.js 中的文件,以默认全局启用漂亮打印。

You can add

DBQuery.prototype._prettyShell = true

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

天邊彩虹 2025-01-09 03:34:15

(注意:这是更新问题的答案)

您可以在 CLI 上执行此操作:

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

并且它总是会输出漂亮的结果。

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

熟人话多 2025-01-09 03:34:15

由于它基本上是一个 javascript shell,因此您还可以使用 toArray()

db.collection.find().toArray()

但是,这将打印集合的所有文档,这与 pretty() 不同,它允许您迭代。
请参阅:http://docs.mongodb.org/manual/reference/method/cursor.toArray/< /a>

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/

我的影子我的梦 2025-01-09 03:34:15

哦,所以我猜 .pretty() 等于:

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

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

db.collection.find().forEach(printjson);
人生百味 2025-01-09 03:34:15

尝试一下 Mongo-hacker(节点模块),它总是打印得很漂亮。
https://github.com/TylerBrock/mongo-hacker

更多它增强了 mongo shell(仅支持 ver>2.4,当前ver 为 3.0),例如

  • Colorization
  • 附加 shell 命令(计数文档/计数文档/等)
  • API 添加 (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), 等)
  • 聚合框架,还没有问题。

我在生产环境中使用的

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.

为人所爱 2025-01-09 03:34:15

回答了这个问题,但无法弄清楚如何从外部加载的 mongo 打印它。所以:

这适用于控制台:并且在控制台中是首选,但不适用于外部mongo加载的javascript:

db.quizes.find().pretty()

这适用于外部mongo加载的javscript:

db.quizes.find().forEach(printjson)

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:

db.quizes.find().pretty()

This works in external mongo-loaded javscript:

db.quizes.find().forEach(printjson)
听你说爱我 2025-01-09 03:34:15

看看这个:

db.collection.find().pretty()

Check this out:

db.collection.find().pretty()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文