从 Rails 控制台,如何查询具有人类可读输出的 mongo 集合?
从 Rails 控制台,我如何轻松查询 mongo 集合,并以人类可读的形式提供漂亮的选项卡样式输出:
Person.all.each { |p| pp p }
这将返回一个接一个打印的每个文档的混乱,但没有一个列是对齐的。
from the rails console, how can I easily query a mongo collection with nice tab style output in a human readable form:
Person.all.each { |p| pp p }
that will return a mess of each document printed one after the other, but none of the cols are lined up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 你有几个选择:
满足于没有列并使用 pp 或非常漂亮的 awesome_print。对于 Awesome_print,我经常这样做:
ap Person.all.map(&:to_mongo)
to_mongo
方法将产生比 pp'ing 或 ap'ing 对象本身更好的输出.自己动手。查看 terminal-table 作为起点。
AFAIK you have a couple options:
Be content without columns and use pp or the very pretty awesome_print. With awesome_print I often do:
ap Person.all.map(&:to_mongo)
Theto_mongo
method will produce nicer output than pp'ing or ap'ing the object itself.Roll your own. Check out terminal-table as a place to start.
这是 JSON,数据不会排列显示,因为文档可能不具有相同数量的“列”。没有架构,因此每个文档可以有不同数量的“列”,但仍然存储在同一个集合下,这就是每个文档单独显示而与其他文档没有关系的原因。
如果您想以特定方式显示它们,您需要获取输出并在您自己的应用程序上对其进行格式化,控制台假设所有文档都具有完全相同的“列”是没有意义的。
This is JSON, data is not displayed lined up because documents may not have the same number of "columns". There's NO schema, so each document can have a different number of "columns" and still be stored under the same collection, That's why each document is displayed on it's own without relationship to other documents.
If you want to display them in a particular way, you need to take the output and format it on your own application, it doesn't make sense for the console to assume all the documents have exactly the same "columns".
我通常这样检查 mongo 集合:
ap Person.pluck(:id, :name, :dreams)
I usually inspect mongo collections this way:
ap Person.pluck(:id, :name, :dreams)