MongoDB 中是否有命名集合的约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9868323/
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
Is there a convention to name collection in MongoDB?
提问by danidacar
I would like to know if there is a convention for database collections such as:
我想知道是否有数据库集合的约定,例如:
PageVisit
or page_visit
.
PageVisit
或page_visit
。
Are there any advantages/disadvantages for these notations?
这些符号有什么优点/缺点吗?
回答by Remon van Vliet
The general conventions are:
一般约定是:
- Lowercase names: this avoids case sensitivity issues, as MongoDB collection names are case sensitive.
- Plural: more obvious to label a collection of something as the plural, e.g. "files" rather than "file"
- No word separators: Avoids issues where different people (incorrectly) separate words (username <-> user_name, first_name <-> firstname). This one is up for debate according to a few people around here, but provided the argument is isolated to collection names I don't think it should be ;) If you find yourself improving the readability of your collection name by adding underscores or camelCasing your collection name is probably too long or should use periods as appropriate which is the standard for collection categorization.
- Dot notation for higher detail collections: Gives some indication to how collections are related. For example, you can be reasonably sure you could delete "users.pagevisits" if you deleted "users", provided the people that designed the schema did a good job ;)
- 小写名称:这避免了区分大小写的问题,因为 MongoDB 集合名称区分大小写。
- 复数:更明显地将某物的集合标记为复数,例如“文件”而不是“文件”
- 没有单词分隔符:避免不同的人(错误地)分隔单词(用户名 <-> 用户名、名字 <-> 名字)的问题。根据这里的一些人的说法,这个有待讨论,但如果该参数与集合名称隔离,我认为它不应该是 ;) 如果您发现自己通过添加下划线或驼峰命名来提高集合名称的可读性集合名称可能太长或应适当使用句点,这是集合分类的标准。
- 更高细节集合的点表示法:给出了集合如何相关的一些指示。例如,如果您删除了“users”,您可以合理地确定您可以删除“users.pagevisits”,前提是设计架构的人做得很好;)
Examples:
例子:
users
pagevisits
users.pagevisits
Field name conventions (should) follow some of the same logic although camel casing those is fairly common.
字段名称约定(应该)遵循一些相同的逻辑,尽管驼峰式大小写这些相当普遍。
回答by AD7six
Just avoid using hyphens in your collection names.
请避免在集合名称中使用连字符。
And that's only because, if you use the cli of the two below calls, the first is invalid JavaScript:
这仅仅是因为,如果您使用以下两个调用的 cli,第一个是无效的 JavaScript:
db.foo-bar.find();
db['foo-bar'].find();
They are both functionally identical, but the second is slightly more annoying to type and doesn't tab-complete.
它们在功能上都是相同的,但第二个打字有点烦人,而且没有制表符完成。
Apart from that, advantages/disadvantages depend on your use of the collections. Being consistent is more important than which convention you choose.
除此之外,优点/缺点取决于您对集合的使用。保持一致比选择哪种约定更重要。
回答by Daniel C. Deng
In http://docs.mongodb.org/manual/reference/limits/, the manual states that the collection names should begin with an underscore('_') or a letter character, and cannot:
在http://docs.mongodb.org/manual/reference/limits/ 中,手册指出集合名称应以下划线('_')或字母字符开头,并且不能:
- contain the $.
- be an empty string (e.g. "").
- contain the null character.
- begin with the system. prefix. (Reserved for internal use.)
- 包含 $.
- 是一个空字符串(例如“”)。
- 包含空字符。
- 从系统开始。字首。(保留供内部使用。)
However, if you follow the rule and create a collection with '_' as the beginning letter, such as "_TWII", you will encounter trouble when you want to drop the collection. See the test below and the way to fix it. Collection '_TWII' was created under 'people' db.
但是,如果按照规则创建一个以'_'为开头字母的集合,例如“_TWII”,那么在想要删除该集合时就会遇到麻烦。请参阅下面的测试以及修复它的方法。集合 '_TWII' 是在 'people' 数据库下创建的。
> show collections
_TWII
employees
system.indexes
> db._TWII.drop()
2015-02-19T16:34:56.738-0800 TypeError: Cannot call method 'drop' of undefined
> use admin
switched to db admin
> db.runCommand({renameCollection:"people._TWII",to:"people.TWII"})
{ "ok" : 1 }
> use people
switched to db people
> show collections
TWII
employees
system.indexes
> db.TWII.drop()
true
> show collections
employees
system.indexes
>
A short-cut to delete _TWII collection while under 'people' db:
在 'people' db 下删除 _TWII 集合的快捷方式:
> db.createCollection('^TWII')
{ "ok" : 1 }
> db.getCollection('^TWII').drop()
true