MongoDB 的命名约定是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5916080/
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:05:13  来源:igfitidea点击:

What are naming conventions for MongoDB?

mongodbnaming-conventions

提问by Andrey

Is there a set of preferred naming conventions for MongoDB entitites such as databases, collections, field names?

是否有一组首选的 MongoDB 实体命名约定,例如数据库、集合、字段名称?

I was thinking along these lines:

我在思考这些问题:

  • Databases: consist of the purpose (word in singular) and end with “db” – all lower case: imagedb, resumedb, memberdb, etc.
  • Collections: plural in lower case: images, resumes,
  • Document fields: lowerCamelCase, e.g. memberFirstName, fileName, etc
  • 数据库:由目的(单数)组成并以“db”结尾——全部小写:imagedb、resumedb、memberdb等。
  • 集合:复数小写:图像,简历,
  • 文档字段:lowerCamelCase,例如 memberFirstName、fileName 等

采纳答案by Tomasz Nurkiewicz

  1. Keep'em short: Optimizing Storage of Small Objects, SERVER-863. Silly but true.

  2. I guess pretty much the same rules that apply to relation databases should apply here. And after so many decades there is still no agreement whether RDBMS tables should be named singular or plural...

  3. MongoDB speaks JavaScript, so utilize JS naming conventions of camelCase.

  4. MongoDB official documentation mentions you may use underscores, also built-in identifier is named _id(but this may be be to indicate that _idis intended to be private, internal, never displayed or edited.

  1. 保持简短:优化小对象的存储SERVER-863。愚蠢但真实。

  2. 我想几乎适用于关系数据库的相同规则应该适用于此。经过这么多年,对于 RDBMS 表应该被命名为单数还是复数,仍然没有达成一致……

  3. MongoDB 使用 JavaScript,因此使用camelCase 的JS 命名约定。

  4. MongoDB 官方文档提到你可以使用下划线,也命名了内置标识符_id(但这可能是为了表明它_id是私有的、内部的、永远不会显示或编辑的。

回答by BBi7

DATABASE

数据库

  • camelCase
  • append DB on the end of name
  • make singular (collections are plural)
  • 骆驼香烟盒
  • 在名称末尾附加 DB
  • 使单数(集合是复数)

MongoDB states a nice example:

MongoDB 给出了一个很好的例子:

To select a database to use, in the mongo shell, issue the use <db> statement, as in the following example:

use myDB
use myNewDB

要选择要使用的数据库,请在 mongo shell 中发出 use <db> 语句,如下例所示:

使用 myDB
使用 myNewDB

Content from: https://docs.mongodb.com/manual/core/databases-and-collections/#databases

内容来自:https: //docs.mongodb.com/manual/core/databases-and-collections/#databases

COLLECTIONS

收藏

  • Lowercase names:avoids case sensitivity issues, 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.

Content from: http://www.tutespace.com/2016/03/schema-design-and-naming-conventions-in.html

  • 小写名称:避免大小写敏感问题,MongoDB 集合名称区分大小写。

  • 复数:更明显地将某物的集合标记为复数,例如“文件”而不是“文件”

  • >无单词分隔符:避免不同人(错误地)分隔单词(用户名 <-> 用户名、
    名字<->名字)的问题。根据
    这里的一些人的说法,这个有待讨论,但如果该参数与集合名称隔离,我认为它不应该是 ;) 如果您发现自己
    通过添加下划线或
    驼峰命名来提高集合名称的可读性name 可能太长或应
    适当使用句点,这是集合
    分类的标准。

  • 更高细节集合的点表示法:给出一些关于集合如何相关的指示。例如,如果您删除了“users”,您可以合理地确定您可以删除“users.pagevisits”,前提是设计架构的人做得很好。

内容来自:http: //www.tutespace.com/2016/03/schema-design-and-naming-conventions-in.html

For collections I'm following these suggested patterns until I find official MongoDB documentation.

对于集合,我遵循这些建议的模式,直到找到官方 MongoDB 文档。

回答by danza

Even if no convention is specified about this, manual referencesare consistently named after the referenced collection in the Mongo documentation, for one-to-one relations. The name always follows the structure <document>_id.

即使没有对此指定约定,手动引用始终以 Mongo 文档中引用的集合命名,用于一对一关系。名称始终遵循结构<document>_id

For example, in a dogscollection, a document would have manual references to external documents named like this:

例如,在一个dogs集合中,一个文档会有对外部文档的手动引用,命名如下:

{
  name: 'fido',
  owner_id: '5358e4249611f4a65e3068ab',
  race_id: '5358ee549611f4a65e3068ac',
  colour: 'yellow'
  ...
}

This follows the Mongo convention of naming _idthe identifier for every document.

这遵循_id为每个文档命名标识符的 Mongo 约定。

回答by Shrinivas Kalangutkar

Naming convention for collection

集合命名约定

In order to name a collection few precautions to be taken :

为了命名一个集合,需要采取一些预防措施:

  1. A collection with empty string (“”) is not a valid collection name.
  2. A collection name should not contain the null character because this defines the end of collection name.
  3. Collection name should not start with the prefix “system.” as this is reserved for internal collections.
  4. It would be good to not contain the character “$” in the collection name as various driver available for database do not support “$” in collection name.

    Things to keep in mind while creating a database name are :

  5. A database with empty string (“”) is not a valid database name.
  6. Database name cannot be more than 64 bytes.
  7. Database name are case-sensitive, even on non-case-sensitive file systems. Thus it is good to keep name in lower case.
  8. A database name cannot contain any of these characters “/, \, ., “, *, <, >, :, |, ?, $,”. It also cannot contain a single space or null character.
  1. 带有空字符串 (“”) 的集合不是有效的集合名称。
  2. 集合名称不应包含空字符,因为它定义了集合名称的结尾。
  3. 集合名称不应以前缀“system”开头。因为这是为内部收藏保留的。
  4. 集合名称中最好不要包含字符“$”,因为数据库可用的各种驱动程序不支持集合名称中的“$”。

    创建数据库名称时要记住的事情是:

  5. 带有空字符串 (“”) 的数据库不是有效的数据库名称。
  6. 数据库名称不能超过 64 个字节。
  7. 数据库名称区分大小写,即使在不区分大小写的文件系统上也是如此。因此,最好将 name 保持为小写。
  8. 数据库名称不能包含以下任何字符“/、\、.、“、*、<、>、:、|、?、$、”。它也不能包含单个空格或空字符。

For more information. Please check the below link : http://www.tutespace.com/2016/03/schema-design-and-naming-conventions-in.html

想要查询更多的信息。请查看以下链接:http: //www.tutespace.com/2016/03/schema-design-and-naming-conventions-in.html

回答by Rex Morgan

I think it's all personal preference. My preferences come from using NHibernate, in .NET, with SQL Server, so they probably differ from what others use.

我认为这都是个人喜好。我的偏好来自在 .NET 和 SQL Server 中使用 NHibernate,因此它们可能与其他人使用的有所不同。

  • Databases: The application that's being used.. ex: Stackoverflow
  • Collections: Singular in name, what it's going to be a collection of, ex: Question
  • Document fields, ex: MemberFirstName
  • 数据库:正在使用的应用程序.. 例如:Stackoverflow
  • 集合:名称单一,它将是一个集合,例如:问题
  • 文档字段,例如:MemberFirstName

Honestly, it doesn't matter too much, as long as it's consistent for the project. Just get to work and don't sweat the details :P

老实说,没有太大关系,只要对项目保持一致即可。只要开始工作,不要担心细节:P

回答by FlappySocks

Until we get SERVER-863keeping the field names as short as possible is advisable especially where you have a lot of records.

在我们得到SERVER-863 之前,保持字段名称尽可能短是可取的,尤其是在您有大量记录的情况下。

Depending on your use case, field names can have a huge impact on storage. Cant understand why this is not a higher priority for MongoDb, as this will have a positive impact on all users. If nothing else, we can start being more descriptive with our field names, without thinking twice about bandwidth & storage costs.

根据您的用例,字段名称会对存储产生巨大影响。无法理解为什么这不是 MongoDb 的更高优先级,因为这将对所有用户产生积极影响。如果不出意外,我们可以开始使用我们的字段名称更具描述性,而无需再考虑带宽和存储成本。

Please do vote.

投票