ArticleDirectory
- II. Introduction of each part
- Iii. source code reading sequence and key points
I. Basic Structure Division
MongoDB can be divided from the bottom up into four parts as a whole:
1. Basic data section
2. General Database Operations
3. Operational features provided by MongoDB
4. database architecture Management
II. Introduction of each part 1. Basic data section
The final job of the database is to store the data and then put forward the data as needed. Therefore, all data must be saved to the memory or hard disk in one form.
There are two main data storage methods in MongoDB.
Bson: similar to an ordered JSON. Is the basis of data storage. Most data in MongoDB uses this data structure to interact with users. Different from general databases, although MongoDB is also a kV operation to read data, the value corresponding to a key can be of different types.
Gridfs: bson can only store up to 4 MB of data. When you need to store large data, you need to use gridfs for storage.
2. General Database Operations
MongoDB also provides these basic functions as a database that includes basic addition, deletion, modification, and query functions and provides auxiliary tools for accelerating basic operations.
Dynamic query: Basic addition, deletion, modification, and query functions. At the same time, MongoDB automatically optimizes these operations during these operations, such as using indexes to read content directly from the cache.
Index: A _ id attribute is created for each piece of data in MongoDB as the most basic index. You can also create an index to improve query efficiency. MongoDB also provides the function of merging indexes, which can be used to merge indexes with multiple keywords.
Geographic location index: You can perform an index based on location-related attributes such as distance.
Pre-query: Before executing a statement, test the time consumption of basic operations such as query, as a test of database design or statement efficiency.
3. Operational features provided by MongoDB
Insert data at the same location: unlike other databases, MongoDB caches data before writing data locally, reading and writing data in the memory is much faster than writing data to the local machine every time.
Map and reduce function: the Special Data Structure of MongoDB determines that it can have different special data processing methods. Writing these two functions can complete the custom functions not provided by many databases.
4. database architecture Management
Data backup: Because of MongoDB's real-time reading and data processing in the memory, a single MongoDB database can easily cause data loss, to ensure that data is not lost or rarely lost (in actual production), MongoDB provides a mechanism to back up data and elect running hosts. (In actual production, it is not necessary for multiple MongoDB servers to run together.) It briefly describes the relationship between the master and slave databases of MongoDB.
Automatic sharding: in actual production, when the data volume is too large to be supported by a MongoDB server, you need to create a MongoDB Database Cluster and each set stores a part of the overall data. This section briefly describes how a mongodbs cluster works:
Iii. source code reading sequence and key points
After learning about the MongoDB cluster mode, I think it is better to read the MongoDB source code from the bottom up.
Cause:
1. First understand the basic data structure, the logicAlgorithmSee the data structure in.
2. algorithms have nothing to do with the running platform, and clusters and other functions are related to the running platform. In order to stay ahead of time and system functions, understanding the design philosophy of MongoDB is more advantageous for understanding deep-level functions.
3. In production optimization, the bottom data structure or algorithm adjustment may be more effective.
Therefore, the following reading sequence is listed:
1. gridfs, bjson
Understand basic data structure
2. Basic Search Functions
Understand basic data processing and input and output rules
3. Index and search optimization
How to create an index and provide its powerful auxiliary search functions.
4. Data Backup
How to synchronize copies of multiple servers
When the master server goes downProgramHow to select a replica server
When the master server recovers, what policy is used to synchronize data again?
5. Automatic multipart search
After a shard is created, how does MongoDB process data jump searches between multiple servers.