默认在 MongoDB shell 中打印漂亮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9146123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:27:53  来源:igfitidea点击:

Pretty print in MongoDB shell as default

mongodbcommand-line-interfacemongo-shell

提问by raffian

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.

有没有办法告诉 Mongo 漂亮的打印输出?目前,所有内容都输出到一行并且难以阅读,尤其是嵌套数组和文档。

回答by Sergio Tulentsev

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

回答by staackuser2

You can add

你可以加

DBQuery.prototype._prettyShell = true

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

到您的文件中$HOME/.mongorc.js,默认情况下全局启用漂亮打印。

回答by Bhanu Chawla

(note: this is answer to the updated question)

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

You can just do this on the CLI:

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

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

And it's always going to output pretty results.

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

回答by Aafreen Sheikh

Since it is basically a javascript shell, you can also use toArray():

由于它基本上是一个 javascript shell,因此您还可以使用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/

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

回答by Goff

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

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

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

回答by Gaurav Gandhi

Give a try to Mongo-hacker(node module), it alway prints pretty. https://github.com/TylerBrock/mongo-hacker

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

More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like

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

  • Colorization
  • Additional shell commands (count documents/count docs/etc)
  • API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
  • Aggregation Framework
  • 着色
  • 额外的 shell 命令(计数文档/计数文档/等)
  • API 添加(db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
  • 聚合框架

I am using for while in production env, no problems yet.

我在生产环境中使用了一段时间,还没有问题。

回答by Witold Kaczurba

Got to the question but could not figure out how to print it from externally-loaded mongo. So:

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

This works is for console:and is prefered in console, but does not work in external mongo-loaded javascript:

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

db.quizes.find().pretty()

This works in external mongo-loaded javscript:

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

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

回答by Mohammad Heydari

Check this out:

看一下这个:

db.collection.find().pretty()