mongodb 我如何描述 Mongo 中的集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6336973/
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 do I describe a collection in Mongo?
提问by Cavachon
So this is Day 3 of learning Mongo Db. I'm coming from the MySql universe...
所以这是学习 Mongo Db 的第 3 天。我来自 MySql 世界...
A lot of times when I need to write a query for a MySql table I'm unfamiliar with, I would use the "desc" command - basically telling me what fields I should include in my query.
很多时候,当我需要为我不熟悉的 MySql 表编写查询时,我会使用“desc”命令 - 基本上是告诉我应该在查询中包含哪些字段。
How would I do that for a Mongo db? I know, I know...I'm searching for a schema in a schema-less database. =) But how else would users know what fields to use in their queries?
我将如何为 Mongo db 做到这一点?我知道,我知道……我正在无模式数据库中搜索模式。=) 但是用户怎么知道在他们的查询中使用哪些字段呢?
Am I going at this the wrong way? Obviously I'm trying to use a MySql way of doing things in a Mongo db. What's the Mongo way?
我这样做是错误的吗?显然,我正在尝试使用 MySql 方式在 Mongo db 中做事。Mongo的方式是什么?
回答by Bharathiraja
Type the below query in editor / mongoshell
在编辑器/ mongoshell 中输入以下查询
var col_list= db.emp.findOne();
for (var col in col_list) { print (col) ; }
output will give you name of columns in collection :
输出将为您提供集合中列的名称:
_id
name
salary
回答by Chris Shain
There is no good answer here. Because there is no schema, you can't 'describe' the collection. In many (most?) MongoDb applications, however, the schema is defined by the structure of the object hierarchy used in the writing application (java or c# or whatever), so you may be able to reflect over the object library to get that information. Otherwise there is a bit of trial and error.
这里没有好的答案。因为没有模式,你不能“描述”集合。然而,在许多(大多数?)MongoDb 应用程序中,模式是由编写应用程序(java 或 c# 或其他)中使用的对象层次结构的结构定义的,因此您可以通过对象库进行反射以获取该信息. 否则会有一些反复试验。
回答by Anurag
This is my day 30 or something like that of playing around with MongoDB. Unfortunately, we have switched back to MySQL after working with MongoDB because of my company's current infrastructure issues. But having implemented the same model on both MongoDB and MySQL, I can clearly see the difference now.
这是我玩 MongoDB 的第 30 天或类似的日子。不幸的是,由于我公司当前的基础设施问题,我们在使用 MongoDB 后切换回 MySQL。但是在 MongoDB 和 MySQL 上实现了相同的模型后,我现在可以清楚地看到不同之处。
Of course, there is a schema involved when dealing with schema-less databases like MongoDB, but the schema is dictated by the application, not the database. The database will shove in whatever it is given. As long as you know that admins are not secretly logging into Mongo and making changes, and all access to the database is controller through some wrapper, the only place you should look at for the schema is your model classes. For instance, in our Rails application, these are two of the models we have in Mongo,
当然,在处理像 MongoDB 这样的无模式数据库时会涉及到模式,但模式是由应用程序决定的,而不是数据库。数据库将推入任何给定的内容。只要您知道管理员没有秘密登录 Mongo 并进行更改,并且所有对数据库的访问都是通过一些包装器进行的,那么您应该查看模式的唯一地方就是您的模型类。例如,在我们的 Rails 应用程序中,这是我们在 Mongo 中的两个模型,
class Consumer
include MongoMapper::Document
key :name, String
key :phone_number, String
one :address
end
class Address
include MongoMapper::EmbeddedDocument
key :street, String
key :city, String
key :state, String
key :zip, String
key :state, String
key :country, String
end
Now after switching to MySQL, our classes look like this,
现在切换到 MySQL 后,我们的类看起来像这样,
class Consumer < ActiveRecord::Base
has_one :address
end
class Address < ActiveRecord::Base
belongs_to :consumer
end
Don't get fooled by the brevity of the classes. In the latter version with MySQL, the fields are being pulled from the database directly. In the former example, the fields are right there in front of our eyes.
不要被课程的简短所迷惑。在使用 MySQL 的后一个版本中,字段是直接从数据库中提取的。在前一个例子中,字段就在我们眼前。
With MongoDB, if we had to change a particular model, we simply add, remove, or modify the fields in the class itself and it works right off the bat. We don't have to worry about keeping the database tables/columns in-sync with the class structure. So if you're looking for the schema in MongoDB, look towards your application for answers and not the database.
使用 MongoDB,如果我们必须更改特定模型,我们只需添加、删除或修改类本身中的字段,它就可以立即工作。我们不必担心保持数据库表/列与类结构同步。因此,如果您正在 MongoDB 中寻找模式,请向您的应用程序寻找答案,而不是数据库。
Essentially I am saying the exactly same thing as @Chris Shain :)
基本上我说的和@Chris Shain 完全一样:)
回答by James Cropcho
I had this need too, Cavachon. So I created an open source tool called Variety which does exactly this: link
我也有这个需要,卡瓦雄。所以我创建了一个名为 Variety 的开源工具,它就是这样做的:链接
Hopefully you'll find it to be useful. Let me know if you have questions, or any issues using it.
希望你会发现它很有用。如果您有任何疑问或使用它有任何问题,请告诉我。
Good luck!
祝你好运!
回答by Rap
While factually correct, you're all making this too complex. I think the OP just wants to know what his/her data looks like. If that's the case, you can just
虽然事实正确,但你们都把这弄得太复杂了。我认为 OP 只是想知道他/她的数据是什么样的。如果是这样的话,你可以
db.collectionName.findOne()
This will show one document (aka. record) in the database in a pretty format.
这将以漂亮的格式显示数据库中的一个文档(又名记录)。
回答by ThinkingInBits
print('\n--->', Object.getOwnPropertyNames(db.users.findOne())
.toString()
.replace(/,/g, '\n---> ') + '\n');
---> _id
---> firstName
---> lastName
---> email
---> password
---> terms
---> confirmed
---> userAgent
---> createdAt
回答by brahmana
AFAIK, there isn't a way and it is logical for it to be so.
AFAIK,没有办法,这样做是合乎逻辑的。
MongoDB being schema-less allows a single collection to have a documents with different fields. So there can't really be a description of a collection, like the description of a table in the relational databases.
MongoDB 是无模式的,允许单个集合拥有具有不同字段的文档。所以不能真正有一个集合的描述,就像关系数据库中表的描述。
Though this is the case, most applications do maintain a schema for their collections and as said by Chris this is enforced by your application.
尽管情况如此,但大多数应用程序确实为其集合维护了一个架构,正如 Chris 所说,这是由您的应用程序强制执行的。
As such you wouldn't have to worry about first fetching the available keys to make a query. You can just ask MongoDB for any set of keys (i.e the projection part of the query) or query on any set of keys. In both cases if the keys specified exist on a document they are used, otherwise they aren't. You will not get any error.
因此,您不必担心首先获取可用的键来进行查询。您可以只向 MongoDB 询问任何一组键(即查询的投影部分)或查询任何一组键。在这两种情况下,如果指定的键存在于文档中,则使用它们,否则不使用。你不会得到任何错误。
For instance (On the mongo shell) :
例如(在 mongo shell 上):
If this is a sample document in your people
collection and all documents follow the same schema:
如果这是您people
集合中的示例文档并且所有文档都遵循相同的架构:
{
name : "My Name"
place : "My Place"
city : "My City"
}
The following are perfectly valid queries :
以下是完全有效的查询:
These two will return the above document :
这两个将返回上述文件:
db.people.find({name : "My Name"})
db.people.find({name : "My Name"}, {name : 1, place :1})
This will not return anything, but will not raise an error either :
这不会返回任何内容,但也不会引发错误:
db.people.find({first_name : "My Name"})
This will match the above document, but you will have only the default "_id" property on the returned document.
这将匹配上面的文档,但返回的文档中只有默认的“_id”属性。
db.people.find({name : "My Name"}, {first_name : 1, location :1})
回答by Gates VP
If you're OK with running a Map / Reduce, you can gather all of the possible document fields.
如果您可以运行 Map/Reduce,则可以收集所有可能的文档字段。
Start with this post.
从这篇文章开始。
The only problem here is that you're running a Map / Reduce on which can be resource intensive. Instead, as others have suggested, you'll want to look at the code that writes the actual data.
这里唯一的问题是您正在运行一个可能占用大量资源的 Map/Reduce。相反,正如其他人所建议的那样,您需要查看写入实际数据的代码。
Just because the database doesn't have a schema doesn't mean that there isno schema. Generally speaking the schema information will be in the code.
正因为数据库没有一个模式并不意味着有是没有模式。一般来说,模式信息会在代码中。
回答by hkasera
I wrote a small mongo shell script that may help you. https://gist.github.com/hkasera/9386709
我写了一个小的 mongo shell 脚本,可以帮助你。 https://gist.github.com/hkasera/9386709
Let me know if it helps.
如果有帮助,请告诉我。
回答by rushUp
You can use a UI tool mongo compass for mongoDb. This shows all the fields in that collection and also shows the variation of data in it.
您可以使用 mongoDb 的 UI 工具 mongo compass。这显示了该集合中的所有字段,还显示了其中数据的变化。