当前位置:网站首页>【MongoDB】4. Usage specification of mongodb

【MongoDB】4. Usage specification of mongodb

2022-06-11 15:20:00 ProphetMo__

【MongoDB】4. MongoDB The standard of use

Today, I looked through the teacher's book and saw MongoDB Several usage specifications of , I think this is very helpful. Write it down first

1. Database usage specification

Databases are represented by names . You should pay attention to the following points about database naming :

  • The encoding format must be UTF-8 character ;
  • An empty string... Is not allowed , namely "";
  • Only use ASCII Letters and numbers in the code table , Do not use underscores (_) Special characters other than ;
  • Database names are case sensitive ;
  • The database name length is limited to 64 Bytes ;
  • The database name cannot be the same as the database name reserved by the system , namely admin、local and config database

Suggestions for database naming :

  1. The database name is recommended to be all lowercase
  2. It is not recommended to use database names starting with numbers
  3. The recommended database naming rule is db_xxxx, That is, the name of "see name, know meaning"

2. Collection usage specification

Collections are identified by name . The following points should be noted about the naming of collections :

  • The encoding format must be UTF-8 character ;
  • An empty string... Is not allowed , namely "";
  • Set names cannot contain \0 character , That is, empty characters , This character represents the end of the set name ;
  • Cannot appear with “System.” The beginning set name , this - Is the reserved prefix for the system collection ;
  • Set naming cannot include the following characters “$”;
  • The length of the set name is limited to 64 Bytes

Based on the above MongoDB Naming conventions for collections , And put forward five suggestions on collection naming from the perspective of actual development :

  1. It is not recommended to use anything other than _( Underline ) and .( spot ) Special characters other than ;
  2. It is recommended that all set names be lowercase ;
  3. It is not recommended to use set names starting with numbers ;
  4. In order to avoid the problems caused by library level lock , Try to write to a larger set using “ Single library single collection ” Structure , For new businesses, try to create new libraries , Instead of creating a new collection in an existing library ;
  5. The recommended set naming rule is t_xxxx;
  6. Use “.” To separate subsets of different namespace , For example, a blog may contain two subsets , namely blog.posts and blog.authors, and blog It doesn't exist

3. Document usage specification

The type of key in the document is generally string type , Keys can be arbitrary UTF-8 character . Note the following points about the naming of documents :

  • The key in the document cannot contain “\0” character , That is, empty characters ;
  • The keys in the document are prohibited from using anything except underscores “” Special characters other than , And it is not recommended to use... At the beginning "";
  • It is recommended that all keys in the document be lowercase ;
  • Keys in documents are not recommended to start with numbers ;
  • It is not recommended to customize “_id” value
  • Try to put similar documents in the same set , Scatter different types of documents in different collections , This can improve index utilization ;
  • It is recommended not to store long strings , If this field is a query condition , Then make sure that the value of the field does not exceed 1KB, because MongoDB Index only supports 1KB Fields within ;
  • It is recommended that if the business is insensitive to the case of stored data , Then use all uppercase / Lower case storage ( Or add an auxiliary field with uniform case );
  • It is not recommended to use array fields as query criteria ;
  • In the consent document , A key with the same name cannot exist ;

Excerpt from 《NoSql Database technology and application 》 Black horse programmer Huang,

原网站

版权声明
本文为[ProphetMo__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012007508799.html