mongodb 如何发现 mongo 数据库的结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14713179/
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
How can I discover a mongo database's structure
提问by tolmark
I have a Mongo database that I did not create or architect, is there a good way to introspect the db or print out what the structure is to start to get a handle on what types of data are being stored, how the data types are nested, etc?
我有一个我没有创建或构建的 Mongo 数据库,有没有一种很好的方法来内省数据库或打印出结构是什么来开始处理正在存储的数据类型,数据类型是如何嵌套的, 等等?
采纳答案by br3w5
Just query the database by running the following commands in the mongo shell:
只需通过在 mongo shell 中运行以下命令来查询数据库:
use mydb //this switches to the database you want to query
show collections //this command will list all collections in the database
db.collectionName.find().pretty() //this will show all documents in the database in a readable format; do the same for each collection in the database
You should then be able to examine the document structure.
然后,您应该能够检查文档结构。
回答by Sammaye
There is actually a tool to help you out here called Variety:
这里实际上有一个工具可以帮助你,叫做 Variety:
http://blog.mongodb.org/post/21923016898/meet-variety-a-schema-analyzer-for-mongodb
http://blog.mongodb.org/post/21923016898/meet-variety-a-schema-analyzer-for-mongodb
You can view the Github repo for it here: https://github.com/variety/variety
您可以在此处查看 Github 存储库:https: //github.com/variety/variety
I should probably warn you that:
我可能应该警告你:
- It uses MR to accomplish its tasks
- It uses certain other queries that could bring a production set-up to a near halt in terms of performance.
- 它使用 MR 来完成它的任务
- 它使用某些其他查询,这些查询可能会使生产设置在性能方面几乎停止。
As such I recommend you run this on a development server or a hidden node of a replica or something.
因此,我建议您在开发服务器或副本的隐藏节点或其他东西上运行它。
Depending on the size and depth of your documents it may take a very long time to understand the rough structure of your database through this but it will eventually give one.
根据文档的大小和深度,可能需要很长时间才能通过此了解数据库的粗略结构,但最终会给出一个。
回答by pratz
This will print name and its type
这将打印名称及其类型
var schematodo = db.collection_name.findOne()
for (var key in schematodo) { print (key, typeof key) ; }
回答by Paul
I would recommend limiting the result set rather than issuing an unrestricted find command.
我建议限制结果集而不是发出不受限制的 find 命令。
use mydb
db.collectionName.find().limit(10)
var z = db.collectionName.find().limit(10)
Object.keys(z[0])
Object.keys(z[1])
This will help you being to understand your database structure or lack thereof.
这将帮助您了解您的数据库结构或缺乏它。
回答by Utsav T
This is an open-source tool that I, along with my friend, have created - https://pypi.python.org/pypi/mongoschema/
这是我和我的朋友一起创建的开源工具 - https://pypi.python.org/pypi/mongoschema/
It is a Python library with a pretty simple usage. You can try it out (even contribute).
它是一个使用非常简单的 Python 库。你可以尝试一下(甚至贡献)。
回答by Michal Jure?ko
One option is to use the Mongoeye. It is open-source tool similar to the Variety.
一种选择是使用Mongoeye。它是类似于 Variety 的开源工具。
The difference is that Mongoeye is a stand-alone program (Mongo Shell is not required) and has more features (histograms, most frequent values, etc.).
不同的是 Mongoeye 是一个独立的程序(不需要 Mongo Shell)并且有更多的特性(直方图、最频繁的值等)。
回答by Ikar Pohorsky
Few days ago I found GUI client MongoDB Compasswith some nice visualizations. See the product overview. It comes directly from the mongodb people and according to their doc:
几天前,我发现 GUI 客户端MongoDB Compass具有一些不错的可视化效果。请参阅产品概述。它直接来自 mongodb 人员并根据他们的文档:
MongoDB Compass is designed to allow users to easily analyze and understand the contents of their data collections within MongoDB...
MongoDB Compass 旨在让用户能够轻松地分析和理解他们在 MongoDB 中收集的数据内容...
回答by user3980196
You can use MongoDB's tool mongodump
. On running it, a dump
folder is created in the directory from which you executed mongodump. In that folder, there are multiple folders that correspond to the databases in MongDB, and there are subfolders that correspond to the collections, and files that correspond to the documents.
您可以使用 MongoDB 的工具mongodump
。运行它时,dump
会在您执行 mongodump 的目录中创建一个文件夹。在那个文件夹中,有多个文件夹对应MongDB中的数据库,有子文件夹对应集合,文件对应文档。
This method is the best I know of, as you can also make out the schema of empty collections.
这种方法是我所知道的最好的方法,因为您还可以找出空集合的模式。