Recently there was a functional module for a project that needed to manipulate collection type data for convenience and other reasons. After much consideration, I finally decided to give up MySQL and choose MongoDB.

You should be familiar with both databases. The biggest difference is that MySQL is a relational database, while MongoDB is a non-relational database. Common relational databases include MySQL, Oracle, DB2, SQL Server, Postgre SQL, etc. Non-relational databases include MongoDB, Redis, Memcached, HBse, etc.

1. Relational databases? Non-relational database?

A relational database can be understood as a database created by relying on a model. For example, the table in MySQL is a two-dimensional table composed of horizontal and vertical columns. Relational databases can use relational models to correlate data from multiple tables, such as one-to-one, one-to-many, and many-to-one. Because it is built on the basis of the data model, so we can easily do complex query operations between multiple tables through SQL statements. Relational databases are relatively secure because they are stored directly on hard disks. Therefore, sudden outages and power outages do not cause data loss. The storage mode of MySQL is determined by its own engine. Common engines are Innodb and MyISAM. The main difference is that MyISAM does not support transactions. The emphasis is on performance, and the execution speed is faster than Innodb, which provides advanced database features such as transaction support.

Non-relational databases, also known as NoSQL databases, are simpler to deploy than relational databases. The storage mode of Mongo is virtual memory + persistent storage. Mongo writes data to memory, and then the virtual memory manager persists it to the hard disk, so the write operation will be much faster than the relational database. NOSQL is stored in the key-value format. It can store basic data types like a relational database, collections, objects, and so on. Although NoSQL has high performance, it does not support objects and cannot perform joint table queries. It is generally used for storing large amounts of data.

What are their advantages and disadvantages

Relational database has been developed for a long time and has a very mature system. Their share is also increasing. And support the operation of things, to ensure the consistency of data, you can complete complex operations through SQL statements. However, when the amount of data reaches a certain level, the efficiency of relational database will decrease obviously. A complex query operation, a series of combined indexes will consume a lot of memory space, at this time we need to separate the database read and write operation, or split the database structure (horizontal split, vertical split) to share the request pressure in different libraries.

Vertical splitting refers to splitting a table into multiple tables that are associated with each other using primary keys. Horizontal split is to split multiple tables according to certain rules, for example, split read/write by user roles. The so-called read/write split is that read operations (query data) and write operations (insert & update) point to different database nodes, and data is synchronized between them through some mechanism, such as binlog. In practice, most of the stress still comes from reading operations, so it’s mostly a master and many slave architecture.

Non-relational databases have become popular in recent years. Free and open source, low cost, simple deployment, unstructured storage and other obvious advantages. And it is very strong on massive data processing capacity, memory level database, query speed is also very fast. The stored data format is rich and easy to expand. Although SQL cannot be used for complex queries, MongoDB supports JavaScript, so complex database management operations can be carried out through JS scripts. My personal feeling about NoSQL is that it doesn’t support things at the moment, otherwise it’s not a problem.

3. When to use Mongo

Mongo is written in c++, supporting a variety of languages such as Java, Python, Ruby, PHP, c++, C#, etc. Sometimes for different business needs, choose Mongo can avoid wasting a lot of unnecessary resources

Logging system

Generally, logs generated during system running have a large variety of types and contents. You can use MongoDB to collect and manage these messy logs. Not only is it easy to manage, but it’s also very easy to find or export

Geographic location storage

MongoDB supports geographical location, two-dimensional spatial index, and can store latitude and longitude, so it can quickly calculate the distance between two points, such as location information. Such as search nearby people, or ordering system, delivery system, etc

The size of the data is growing fast

As mentioned above, when the amount of data in a relational database is too large, it is necessary to divide the database into tables, which may be more troublesome to operate. If you choose Mongo to operate the database and table, it will become very simple.

Ensure a highly available environment

Mongo itself has a high availability and partitioning solution, it is very convenient to set up master and slave servers, in addition to Mongo can quickly and safely achieve the transfer of failed nodes.

File storage requirements

GridFS is the MongoDB specification for storing and retrieving large files such as images, audio, and videos. GridFS, though, is one way of storing files, and can store files larger than 16 megabytes. But it itself is stored in the MongoDB collection

Other scenarios

For example, in game development, we can use MongoDB to store user information, equipment, points, etc. In addition, Mongo can provide perfect data storage services for logistics system, social system and even Internet of Things system.