MongoDB is a popular open source document database, and judging by its name, it does have some ambitions. The original name of MongoDB originally comes from the English word “Humongous”, which means “huge” in Chinese, meaning that the intention of the namers is to process large amounts of data.
Although the selection of a document database can be daunting for some teams, it still has some support, such as ranking on DB-Engine:
MongoDB has long been ranked 5th in the overall ranking (1st in document database) and is also the most popular NoSQL database. MongoDB also has an active community and a commercial drive (MongoDB went public on NASDAQ in 2017) to drive the growth of the open source database.
Before entering the body, first share a MongoDB material “In-depth study MongoDB”. It’s 170 pages!
The book is divided into four main themes:
1. Configure the MongoDB cluster by sharding.
2. Query and update data in the cluster.
3. Operate, monitor, and back up clusters.
4. From the perspective of program design, consider how to deal with sharding, configuration server or MongoDB process stop.
Not only from the operation and maintenance aspects of cluster management, but also from the application design, implementation, optimization, data management and security aspects of the use of MongoDB to build application skills, from 50 development examples analysis of MongoDB to build application skills, dry goods full!
Click the link below to obtain information [Web disk link and extraction code] :
Iiv.h5.xeknow.com/s/3rRX15 【 copied to open more convenient 】 V.X
Enter the text!
Mongo:
It is an in-memory database where data is stored.
Most of the operations on the data are in memory, but MongoDB is not a pure in-memory database. -MongoDB is an open source database system based on distributed file storage, written in C++ language.
In the case of high load, tianjian has more nodes to ensure the performance of the server.
MongoDB aims to provide scalable, high-performance data storage solutions for WEB applications.
MongoDB stores data as a document, and the data organization consists of key=>value pairs. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.
Storage features of MongoDB
In traditional relational databases, data is stored as a form, and each form has vertical columns and horizontal columns. Thus, compared with MySQL, MongoDB completes data storage in an intuitive document way. It is similar to the JSON format defined in JavaScript, except that MongoDB adds serialization to the document while the data is stored, and the document is eventually saved to disk ina format called BSON, or binary-JSON.
In the data stored in the database, there is a special key value called the primary key, which is used to uniquely identify a record in the table. That is, a table cannot have more than one primary key, and the primary key cannot be null.
Primary key definitions exist in both MongoDB and MySQL
In the case of MongoDB, the primary key is’ _id ‘. If the user does not actively assign a primary key to the user, MongoDB automatically generates a randomly assigned value for the user. In MySQL, the PRIMARY KEY is specified by specifying the PRIMARY KEY when MySQL inserts data. When no primary key is specified, another tool, an index, replaces the primary key. Indexes can be empty or duplicate, and there is a unique index that does not allow duplicates. If neither primary key nor index is specified, MySQL automatically creates one for the data.
-
Average database insert rate: MongoDB with no specified _ID >MySQL with no specified primary key >MySQL with specified primary key >MongoDB with specified _id
-
In MongoDB, specifying index inserts is much slower than not specifying them because the _ID value of each piece of data in MongoDB is unique. If the id is not specified, the id is automatically calculated by the system. MongoDB uses computer eigenvalues, time, process IDS, and random numbers to ensure that the generated _ID is unique. When the _id is specified, MongoDB needs to check whether the _ID is available for each data insert. If there are too many data inserts in the database, the query cost in this step will slow down the whole database insert speed.
-
MongoDB makes full use of system memory as a cache, which is a very nice feature. The memory of our test machine is 64G. When inserting, MongoDB will try its best to store data on the hard disk after the data cannot be written into the memory quickly. This is why MongoDB is far more efficient when the _ID is not specified. However, when the specified _id is inserted, when the amount of data is too large for the memory, MongoDB needs to read the information from the disk into the memory to check the data, which slows down the insertion efficiency.
-
MySQL is a very stable database, and is not much less efficient with or without primary key inserts.
Application scenarios of MongoDB
On the other hand, for developers, if the format of the data is not clearly defined due to business requirements or the initial phase of the project, MongoDB’s distinctive features stand out. Compared to traditional relational databases, it is much easier to expand, which makes writing code much easier. However, MongoDB has weak support for transaction relationship between data. If business requirements are high, MongoDB is not suitable for such applications. A non-relational database (NoSQL) is a document database. Let’s start by explaining the document database, which can hold XML, JSON, BSON, that type of data. These data are self-describing and present a hierarchical tree data structure. The data structure consists of key=>value pairs.
Storage mode: Virtual memory + Persistence
Persistent mode:
All MongoDB data is actually stored on hard disks, and all data to be operated is mapped to a certain area of memory through MMAP. MongoDB then makes data changes in this area, avoiding piecemeal hard drive operations. It is up to the operating system to flush the Mmap data to hard disk, so if MongoDB flush the Mmap data in memory and the system fails before the data is flushed to hard disk, the data is lost.
The main features
- MongoDB provides a document-oriented storage, which is relatively easy and simple to operate.
- You can index any attribute in the MongoDB record (e.g., FirstName=”Sameer”,Address=”8 Gandhi Road”) for faster sorting. Data mirroring can be created locally or over the network, which makes MongoDB more scalable.
- If the load increases (requiring more storage space and more processing power), it can be distributed among other nodes in the computer network, which is called sharding.
- MongoDB supports rich query expressions. Query instructions use JSON-style tags to easily query objects and arrays embedded in documents.
- MongoDB uses the update() command to replace completed documents (data) or some specified data fields.
- Map/ Reduce in MongoDB is used to process and aggregate data in batches.
- Map and Reduce. The Map function invokes emit(key,value) to traverse all records in the set and send the key and value to the Reduce function for processing.
- The Map and Reduce functions are written using JavaSript, and mapReduce operations can be performed using db.runCommand or mapReduce command.
- GridFS is a built-in feature in MongoDB that can be used to store a large number of small files.
- MongoDB allows script execution on the server side. You can write a function in JavaScript and execute it directly on the server side. You can also store the function definition on the server side and call it directly next time.
- MongoDB supports a variety of programming languages :RUBY, PYTHON, JAVA, C++, PHP, C# and more.
monitoring
- MongoDB provides the network and system monitoring tool Munin, which is applied to MongoDB as a plug-in.
- Gangila is a high-performance system monitoring tool for MongoDB that is used as a plug-in.
- Cacti, an open source tool based on a graphical interface for viewing CPU load and network bandwidth utilization, also provides a plug-in for monitoring MongoDB.
- Query statement: it is a unique MongoDB query method.
- Suitable for scenarios: event logging, content management or blogging platforms, etc.
- Architecture features: High availability can be achieved through replica sets, as well as sharding.
- Data processing: Data is stored on the hard disk, but the data that needs to be read frequently is loaded into memory and stored in physical memory for high-speed read and write.
- Maturity and breadth: emerging database, low maturity, No SQL database is the closest to relational database, one of the more perfect DB, suitable for the growing population.
Mongo advantages:
- Superior performance: fast! MongoDB’s performance at the appropriate level of memory is very fast. It stores hot data in physical memory, making hot data read and write very fast.
- High scalability: Rich third party support (this is also an advantage of MongoDB compared to other No SQL)
- Its own Failover mechanism!
- Weak consistency (final consistency) ensures faster user access
- The storage method of document structure makes it easier to obtain data: storage format of JSON
- Support large storage capacity, built-in GridFS
- Sharding
ODB faults:
This is mainly transaction-free
- MongoDB does not support transactions (major disadvantage)
- MongoDB occupies too much space
- MongoDB does not have as sophisticated a maintenance tool as MySQL, which is a notable point for both development and IT operations
Redis:
It is literally an in-memory database. Persistence: Redis keeps all data in memory and uses RDB or AOF for persistence.
MySQL:
Both data and indexes are stored on hard disk. Swap it into memory when it’s time to use it. The ability to process far more data than the total amount of memory. Relational databases. There are different storage methods on different engines. Query statement is the use of traditional SQL statements, with a relatively mature system, high maturity. The share of open source databases continues to grow, and the share page of MySQL continues to grow.
The downside is that processing large amounts of data can be significantly slower.
Comparison of data volume and performance:
When physical memory is insufficient, both Redis and MongoDB use virtual memory. In fact, if Redis were to start virtual memory, it would be obvious to either add a memory stick or you’d have to switch databases.
However, MongoDB is different. As long as the service can ensure the read/write ratio of hot and cold data, the exchange of hot data in physical memory is less than that of MMAP. MongoDB can still guarantee performance.
Someone used MongoDB to store data on T. MySQL, MySQL doesn’t have to worry about the amount of data in memory. However, the amount of memory in relation to hot data can greatly affect performance. When you run out of physical memory and virtual memory, you probably don’t have much choice but MySQL. In fact, I prefer to classify MongoDB as a hard disk database in terms of data storage principles, but use Mmap as a means of acceleration.
Comparison between DB and MySQL
1. Stability
2, index, index in memory, can improve the performance of random read and write. If indexes are not fully in memory, MongoDB performance deteriorates dramatically as frequent disk swaps occur when random reads and writes are high
3. It takes up a lot of space, because it belongs to the type of typical space-for-time principle. Therefore, its disk space is more wasted than that of ordinary database, and so far it has not realized online compression function. When frequently adding, deleting and modifying data in MongoDB, if records change, such as data size changes, it is easy to generate some data fragments, resulting in fragmentation. One is that indexes will have performance problems, and the other is that after a certain period of time, the space occupied will increase inexplicably, so it is necessary to repair the database regularly and re-index regularly, which will improve the stability and efficiency of MongoDB. In the latest version, it has implemented online compression, estimated around version 2.0, should be able to achieve online compression, can perform some operations in the background now repair DataBase. That would solve the big problem that’s troubling us right now.
4. MongoDB has weak support for transaction relationship between data
5. Inconvenient operation and maintenance
ODB’s advantages over MySQL
Suitable for requirements models where the specific data format of the database is unclear or frequently changes, and developer friendly.
ODB comes with a distributed file system that can be easily deployed on a server farm. MongoDB has a Shard concept, which is convenient for the use of server sharding. With each additional Shard, MongoDB’s insert performance increases by nearly a multiple, and disk capacity can be easily expanded.
It supports the Map-Reduce computing framework, which facilitates data statistics. The traditional relational database is generally composed of database, table and record. MongoDB is composed of three levels: database, collection and document. MongoDB has no concept of column, row and relation in the set of tables in relational database, which reflects the characteristic of schema freedom.