In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about the naming and design specifications of the MongoDB database, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Document
Design constraint
UTF-8 character
Cannot contain\ 0 character (empty character), which identifies the end of the building.
. And $have a special meaning and need to be avoided
Case sensitive
Keys cannot be repeated
Order of key / value pairs
Practical constraint
[mandatory] key in the document forbids the use of special characters other than _
[forced] key is all lowercase, and multiple words can be underlined
[mandatory] prohibit the use of numeric prefixed key
[force] disable custom _ id (_ id is generally self-increasing, and the use of unordered id is very likely to degrade write performance)
[suggestion] putting similar types of documents in a collection can greatly improve index utilization.
[recommended] if the business is not sensitive to the case of data storage, use all uppercase / lowercase storage (or add an auxiliary field with uniform case). Using case-insensitive queries is extremely performance-intensive
[recommended] do not store too long strings
Note: MongoDB index only supports fields within 1KB.
[mandatory] prohibit the use of numeric prefixed key
[force] disable custom _ id (_ id is generally self-increasing, and the use of unordered id is very likely to degrade write performance)
[suggestion] putting similar types of documents in a collection can greatly improve index utilization.
[recommended] if the business is not sensitive to the case of data storage, use all uppercase / lowercase storage (or add an auxiliary field with uniform case). Using case-insensitive queries is extremely performance-intensive
[recommended] do not store too long strings
Note: MongoDB index only supports fields within 1KB.
Set design constraint
UTF-8 character
Cannot be an empty string (")
Cannot contain\ 0 character (null character), which marks the end of the collection name
You can't use "system." At the beginning, this is the prefix reserved for the system
Does not include the character "$" in the collection
Use "." To separate subsets of different namespaces, for example, a blog may contain two subsets, blog.posts and blog.authors, while blog itself may not exist
Practical constraint
Forbid the use of special characters other than _
[mandatory] the collection name does not exceed 64 characters
[force] all lowercase collection names
[mandatory] prohibit the use of numeric collection names, prohibit the use of system collection names (system is the system collection prefix)
[suggestion] in order to avoid the problems caused by library-level locks, we should try to use the structure of "single library and single collection" for writing large collections, so for new businesses, we should try to create new libraries instead of creating new collections in existing libraries.
Database design constraints
UTF-8 character
Cannot be an empty string (")
Basically, you can only use letters and numbers in ASCII.
Cannot contain /,\,., ", *,:, |,?, $, (space),\ 0 (empty character)
All in lowercase. (uppercase is supported, not recommended)
No more than 64 bytes
There are reserved databases such as admin, local, config
Note: the database name restriction is mainly due to the fact that the database will eventually become a file in the file system, and the database name is the corresponding file name, so there are many constraints.
Practical constraint
[mandatory] Database names are all lowercase
[mandatory] the database name does not exceed 64 characters
Forbid the use of special characters other than _
[mandatory] it is forbidden to use database names that start with numbers
[mandatory] duplicate names of retained databases are prohibited, such as admin,local,config, etc.
Indexes
[mandatory] the length of the index name does not exceed 128 bytes
[force] prohibit the creation of indexes on array fields
[force] when creating a composite index, try to put the fields with a large data base (unique data with more values) in front of the composite index
Query
[suggestion] first do the equivalence query, then sort, and then do the range query.
[recommended] some $operators in the query may lead to poor performance, so try to avoid
Note:
Ne,ne, ne,ne,not,exists,exists, exists,exists,nin,$or, try not to use it in the business.
$exist: because of the loose document structure, the query must traverse every document.
$ne: if the negative value is most, the entire index will be scanned
$not: may cause the query optimizer to not know which index to use, so it often degenerates to a full table scan
$nin: full table scan
$or: query as many conditions as there are, and finally merge the result set, so use $in as much as possible
Namespace
Before adding the database name to the collection, you get the fully qualified name of the collection, called the namespace, such as the blog.posts collection of the cms database, which is:
Cms.blog.posts . The length of the namespace in actual use must not exceed 100 bytes.
The second part is the design specification. Database design specification
The database name is supposed to be lowercase.
The database name cannot contain special characters other than'_', for example: /\. "$.
The database name is up to 64 characters.
The launch of the database needs to be reviewed by DBA.
Collective design specification
The collection name is by convention lowercase.
Collection names cannot contain special characters other than''; collection names are prohibited with system. The beginning.
The maximum length of the collection name is 64 characters, including the [database.] content of the prefix.
The naming rules for collection names are the same as those for MySQL database tables. A) the collection of the same module uses the same prefix name as much as possible, and the collection name expresses the purpose as much as possible. B) data tables such as order_header, order_detail
C) coding table base_ d) log table log
Fixed sets can be used to record logs, which insert data faster and eliminate the earliest data when inserting data. Fixed collections need to be explicitly created, specifying the size of the Size, and the number of documents. No matter which limit the collection reaches first, the new document inserted later will move the oldest document out.
Index name: idx. If the field name is too long, you can use the field abbreviation.
Document design specification
Key naming convention: cannot start with $; cannot contain. (dot).
The _ id key in the document recommends the default value, which forbids saving custom values to _ id. Every MongoDB document has a "_ id" key, which defaults to an ObjectID object (the identifier contains a timestamp, machine ID, process ID, and counters). MongoDB differs greatly between specifying _ id and not specifying _ id insertion, and specifying _ id slows down the insertion rate.
Short field names are recommended. Unlike relational databases, each document in the MongoDB collection needs to store field names, and long field names require more storage space.
It is prohibited to store data of multiple data types in the same collection field.
If the date type is selected as string, documents with different date formats do not support equivalence query and range query. Create a test collection product and insert Date: "20180425" and Date: "2018-04-25" data into the collection, respectively. Equivalence query and range query ($gte, $lte) can only find data with the same date format, all of which are one piece of data.
MongoDB is case-sensitive, and if the field does not need to be case-sensitive, in order to improve query efficiency, try to unify the case before inserting it into the database.
MongoDB is a document database, and the data is stored in the document in the form of BSON. MongoDB can support a document size of up to 16 MB. It is recommended that you try not to store large objects and keep the document within 16 MB.
The array size is queried through $size, but the $size operator does not use indexing and restriction exact matching (the $Sized range cannot be specified). Therefore, if you need to execute a query based on the size of the array, you can add the size attribute to the document design. For example, in commodity evaluation, others can vote on the evaluation. To prevent users from voting multiple times and sorting helpful comments, the evaluation document is designed to store the ID of all comment users in an array field (voter_ids), while the array size is cached in the helpful_votes field.
The sharding key must have an index, and the size of the sharding key is limited to 512byte. Once the collection has been sharded, the sharding key cannot be modified directly. Inserting a document without a sharding key into a sharded collection is not accepted, and null insertion is not supported.
Design principles of chip keys:
A) all inserts, updates and deletions will be uniformly sent to all shards in the cluster.
B) all queries will be distributed evenly across all shards in the cluster.
C) all updates or deletions will be directed only to the relevant shards and will not be sent to a shard that does not store the modified data.
D) A query will not be sent to a shard that does not store the queried data.
Connection specification
Properly connect replica sets, which provide mechanisms for data protection, high availability, and disaster recovery. If the master node goes down, one of the slave nodes is automatically promoted to a slave node.
Reasonably control the size of the connection pool and limit the number of connection resources. You can configure the connection pool size through the maxPoolSize parameter in Connection String URL. The service model of Mongod is that each network connection is handled by a separate thread, and each thread is configured with the stack space of 1MB. When there are too many network connections, too many threads will lead to more context switching overhead and memory overhead.
Replication set read option. By default, all read requests for the replication set are sent to Primary,Driver to route read requests to other nodes through the set Read Preference.
A) Primary: default rule, all read requests are sent to Primary.
B) PrimaryPreferred: Primary first. If Primary is unreachable, request Secondary.
C) Secondary: all read requests are sent to Secondary.
D) SecondaryPreferred:Secondary first, request Primary when all Secondary is unreachable.
E) Nearest: the read request is sent to the nearest reachable node (the nearest node is detected by ping).
Operating standard
There are two ways to update documents in MongoDB database-document replacement and target field update. You can either completely replace an existing document or use the update operator to modify a field. Using operators, such as the $set operator and the $push operator, you can update the specified fields in the document regardless of the original size. In scenarios with frequent document updates, using target updates can take less time to serialize and transfer data and achieve better performance.
Multiple document updates, by default, only the first document that matches the querier is updated. To update all matching documents, you need to explicitly specify the multiple document update mode-add the parameter multi:true.
Updates are atomic at the document level, which means that a statement that updates 10 documents may fail for some reason after updating 3 documents. The application must handle these failures according to its own policy.
Update combined with upsert can be used to process, update when the document exists and insert when the document does not exist. If the query selector matches, the update is performed normally; if there is no matching document, a new document is inserted. The fields of the new document are a logical merge of the query selector and the target update document.
Data security and write policies for replication sets, and Write Concern is used to control the level of write security. Write Concern is a tradeoff between performance and data consistency and should be set according to the business scenario. For strong consistency scenarios, w > 1 or equal to majority is recommended.
The aggregation framework is a high-level query language of MongoDB, which allows the transformation and merging of data from multiple documents to generate document information that does not exist in a new single document. The aggregation framework of MongoDB can be equivalent to the Group By statement of SQL.
After reading the above, do you have any further understanding of the naming and design specifications of the MongoDB database? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.