MongoDB: 1. What is MongoDB? Advantages and disadvantages of MongoDB

The entire content of this article is based on MongoDB4.2 version.


1. What is MongoDB?

  MongoDB is a scalable, open source, free table structure, document-oriented high-performance distributed database written in C++. It is more like a database between a relational database (RDBMS) and a non-relational database (NoSQL). It is the non-relational database with the most functions in the non-relational database and the most similar to the relational database. The data structure supported by MongoDB is very loose, and it is a bson format similar to json, which can store more complex data types.

1. Concepts in MongoDB

  For the convenience of understanding, the following table compares Mysql with MongoDB. The table in Mysql corresponds to the collection concept in MongoDB, the row in Mysql corresponds to the document concept in MongoDB, and the concept of database and index is consistent with Mysql.

Mysql Mongodb
database database
table collection
row document
index index

2. Advantages and disadvantages of MongoDB

  First quote the first sentence of the official document:

Welcome to the MongoDB 4.2 Manual! MongoDB is a document database designed for ease of development and scaling
Translated into Chinese is:
Welcome to the MongoDB 4.2 Manual! MongoDB is a document database designed for ease of development and scaling.

So what MongoDB wants to provide at the beginning of its design is a database that can simplify development and support expansion, and it has the characteristics of high performance and high availability.

①Simplified development: data is stored in documents in the form of key-value pairs similar to JSON files. This design pattern has fewer constraints on data, and documents correspond to native data types in many programming languages, which is very convenient for development .
②Extensibility: Traditional vertical expansion is limited to the performance of a single server. The MongoDB sharding mode supports horizontal expansion, and the database can be expanded by increasing the number of servers.
③High performance: Under the WiredTiger storage engine, the data is written into the memory immediately, and the operation of persisting to the hard disk is completed asynchronously in the background. At the same time, the read-write separation of the replica set and the multi-write mode of the sharding mode can improve the throughput to a greater extent.
④ High availability: The replica set mode will synchronize data in one or more replica servers, and supports automatic failover, and provides redundant backup of data, improving data availability and ensuring data security.

  Although MongoDB has obvious advantages and introduced distributed transactions in version 4.2, it is still not recommended to use MongDB for applications with strong transactional requirements. In addition, connecting different types of data in multiple different dimensions is a relational It is also not recommended to use MongoDB for the things that the database is good at.

Two, the characteristics of MongoDB

1. Nested Document Model

   MongoDB's documents (a document in Mongo can be imagined as a piece of Mysql data for easy understanding) store data in a Json-like key-value pair format, which is called Bson in Mongo. And MongoDB documents can embed the document structure into fields or arrays in the document, as shown in the figure below.
insert image description here
   The nested document model allows applications to store related pieces of information in the same database record. As a result, applications can perform common operations with fewer queries and updates, improving performance. And this denormalized data model facilitates atomic operations by combining all related data in a single document, rather than normalizing across multiple documents and collections.

2. Mode freedom

   In a relational database, we must first declare a table structure, and by default in MongoDB, documents in a collection (which can be understood as approximately equal to tables in Mysql) are not required to have the same fields, and the same fields are in different documents There can also be different data types in . As shown below, the first piece of data is name, age, and gender, and the second piece of data is gender, age, and height.

> db.test.insert({
    
    "name":"xiaoming","age":"18","sex":"男"});
WriteResult({
    
     "nInserted" : 1 })
> 
> db.test.insert({
    
    "name":"xiaohong","age":"16","height":"165"});
WriteResult({
    
     "nInserted" : 1 })

> 
> db.test.find()
{
    
     "_id" : ObjectId("62b49cda779f3b59142bb909"), "name" : "xiaoming", "age" : "18", "sex" : "男" }
{
    
     "_id" : ObjectId("62b49d25779f3b59142bb90a"), "name" : "xiaohong", "age" : "16", "height" : "165" }
> 

   Starting from version 3.2, MongoDB introduces specified validation rules, that is, data validation is performed according to artificial validation rules when inserting or updating. In version 3.6, the validation rules of Json mode were introduced.

3. Reliability

   MongoDB supports replica set mode, providing real-time data backup and automatic failover.
   The smallest replica set cluster needs to be deployed on three servers, namely the primary node (Primary), the secondary node (Secondary), and the arbitration node (Arbiter). The master node will synchronize the data to the slave node in real time to realize data backup. If the master node is abnormal, the slave node will be upgraded to the master node to realize automatic failover.
insert image description here

4. Scalability

   When considering increasing the load on a system, it is common to think about vertical scaling or horizontal scaling. Vertical expansion means adding resources such as CPU, memory, and storage to a single server. This expansion method seriously challenges the hardware limitation of a single server, and the expansion cost is relatively high. Horizontal expansion means increasing the number of servers to increase the system's carrying capacity. It is not limited by the hardware of a single server. Multiple servers work together, but the operation is more complicated than vertical expansion.
   MongoDB's sharding mode is a way to distribute data across multiple machines. Sharding patterns can be used to support very large datasets and high-throughput operations. The components of the sharding mode are shown in the figure below.
insert image description here

5. Support distributed transactions

   For situations where atomic operations on multiple documents are required, MongoDB began to support multi-document transactions in replica set mode in version 4.0, and version 4.2 began to support transactions in fragmented clusters, realizing distributed transactions. So far, MongoDB can also achieve a comprehensive replacement Use of relational databases. Using distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

6. File Storage

   BSON limits a document to store up to 16MB of data. If our file is less than 16MB, we can convert the file into binary data and store it in the collection as a document. At the same time, MongoDB also provides a file specification for storing and retrieving more than 16 MB, namely GridFS.
   GridFS splits files into blocks and saves them in multiple documents. This way, we don’t need to load the entire file into memory when we want to read part of the file information. It may be more efficient than the system-level file system when facing large files. . GridFS can be used to store not only files over 16MB, but any file, but MongoDB recommends that if all the files we want to store are less than the 16MB limit, we should give priority to storing each file in a single document instead of GridFS .


reference

"MongoDB Core Principles and Practice"
Mongodb official document by Guo Yuanwei: https://www.mongodb.com/docs/v4.2/

Guess you like

Origin blog.csdn.net/qq_16583855/article/details/125292258