Preface:

Recently read MongoDB related knowledge, just began to hear MongoDB, a face meng circle, this is what a database ah.

But after studying, I found that this database is pretty awesome; Heh heh…

Let’s take a look at the database and see what’s so great about it.

Main line of this article:

① Introduction to MongoDB

What features does MongoDB support?

③ a brief description of MongoDB storage engine

4. MongoDB knowledge expansion

Directing a summary:

MongnDB is a distributed file storage database or document database;

MongoDB is a product between relational database and non-relational database. Among non-relational database, it has the most rich function, most like relational database and most close to relational database.

MongoDB data logical structure:

First of all, let’s talk about MongoDB’s data logic levels: database, Collection and document.

The relationship between logical levels of MongoDB data is shown as follows:

Comparison of data logical structure between MongoDB and relational database:

The logical structure relationship between MongoDB and relational database is shown in figure 1.

Database —— Database, Table —— Collection Collection, Row records —— Document Document

{“name” : “lyl”, “age” : 25} Document {“name” : “lyl”, “age” : 25}

Data storage structure of documents in MongoDB:

The file data storage structure of MongoDB is BSON. Bson, short for Binary JSON, is the Binary encoding serialization of jSON-like documents.

Characteristics of Bson:

  • Lightweight sex
  • ergodicity
  • High efficiency

What about MongoDB’s Bson format document data?

Now that we have a basic understanding of MongoDB as a whole, let’s talk a little bit about what it does. Find out what awesome features it supports that keep it popular.

MongoDB supports the following features:

Let’s post a big picture from the web: it describes the main features of MongoDB;

In addition to the above features, MongoDB supports many powerful features:

  • BSON document model:
    • Dynamic data schema
    • Use efficient binary data storage, including large objects (such as videos).
  • A dynamic query
  • Full-text search
  • Aggregation operation:
    • Polymerization pipe
    • Map – Reduce
    • Single-purpose polymerization method
  • Horizontal expansion (high expansion) :
    • Primary/secondary replication (Setting up a primary/secondary replication cluster)
    • Data sharding (Build a data sharding cluster)
    • Data sharding + master-slave replication = more powerful clustering system
  • High availability and fault recovery
  • Support large file storage GridFS
  • Enterprise security
  • Support for powerful indexes:
    • Single field index
    • Review the index
    • Many key index
    • The text index
    • Wildcard index
    • Geographical index
    • The hash index
    • Index features:
      • TTL index
      • The only index
      • Part of the index
      • Sparse index
      • The index of overlap
  • Version 4.0 supports multi-document transactions and version 4.2 supports distributed transactions
  • Supports multiple storage engines
    • MMAP storage engine
    • MMAPV1 storage engine
    • WiredTiger storage engine
    • In-memory storage engine

Note: This is just a list of the main MongoDB features. For details on each feature, check out the resources section.

Let’s talk about the important storage engine in MongoDB, because the storage engine is the main component of MongoDB responsible for managing data, and transactions, concurrent locking, data storage operations are supported by the storage engine.

MongoDB’s storage engine:

MongoDB supports a variety of storage engines. Let’s briefly talk about these storage engines.

Native storage engine MMAP:

MMAP is the Memory Mapped Storage Engine. It was used prior to version 3.0.

MMAP can map some or all of the contents of a disk file directly into memory, so that the location of information in the file has the corresponding address space in memory. In this case, the file can be read and written using Pointers, without the need for read/write functions. But this does not mean that the file is mapped to physical memory. Only when this piece of data is accessed will it be converted to physical memory by the operating system as a Page.

During MongoDB write operations, data is stored in memory first, and then data is refreshed to disk through sequential I/O, which greatly improves write performance. MongoDB also simplifies MongoDB’s work by handing over memory management to the virtual memory manager of the operating system.

Native upgrade MMAPV1 storage engine:

In MongoDB3.0, MMAPV1 storage engine was introduced, which is a new storage engine updated and upgraded on the basis of the native storage engine.

There is an important log file in the MMAPV1 storage engine: journal pre-write transaction log, which is used for data persistence and database recovery in the event of a hard database shutdown.

Plug-in WiredTiger storage engine:

WiredTiger was introduced in MongoDB3.0 and started as MongoDB’s default storage engine in MongoDB3.2.

Compared to MMAPv1, WiredTiger storage engine is more powerful and has higher performance.

The WiredTiger storage engine also has journal pre-written transaction logs to ensure data persistence and data recovery in the event of database failures.

In-memory storage engine:

The in-memory storage engine stores all database data In Memory and stores only a small amount of metadata, diagnostic logs, and temporary data to disk files, avoiding disk I/O operations and fast query.

If MongoDB uses an in-memory storage engine, it is very similar to the Redis in-memory database.

Comparison between storage engines:

The following is a comparison of MMAPV1, WiredTiger, and In-memory to clarify the differences between storage engines.

Click to see a larger version:

Note: As you read through this article, be sure to use your own version of MongoDB, as different versions may support different features. Make sure your version supports the desired features.

MongoDB knowledge extension:

Why eat memory?

When using the MongoDB database, it is found that the available memory of the server installed with MongoDB has been decreasing. Through searching, it is found that MongoDB has been continuously eating memory when using, and MongoDB occupies more and more memory.

Why does MongoDB take up so much memory?

The following is simply from two aspects:

  1. For example, the WiredTiger storage engine writes data to the Cache, saves data to the memory, and then removes the data from the memory to the disk. However, the data in the memory is still saved, but the data that has been removed from the disk is marked.
  2. When a large number of concurrent requests are made to the MongoDB database, its memory usage also increases; Because when there are many concurrent connections, a large number of threads will be created to process connections, and these threads will also consume memory. In addition, after establishing a good connection, processing the data packets transmitted in the connection, the storage of these data packets also needs to occupy memory; However, this memory footprint is slowly released as requests drop.

How to control memory usage:

The following is a brief discussion on how to control MongoDB’s memory usage in two aspects. Hopefully, the server will not run out of memory alarm.

  1. The reasonable configurationWiredTiger cacheSizeGBParameters:
    • If only MongoDB is deployed on a machine and MongoDB can use all available memory, use the default configuration.
    • If MongoDB and other process services are deployed on a machine, cacheSizeGB needs to be configured based on the memory quota allocated to MongoDB, or 60% of the memory quota can be configured.
  2. Control the number of concurrent requests to MongoDB and make a reasonable number of TCP connections.

Application scenarios of MongoDB:

Based on MongoDB’s features and supported functions, let’s briefly talk about its applicable application scenarios:

  • Application server log storage.
    • MongoDB’s high performance is enough to support more than 2-3 times of TPS/QPS of relational databases;
    • Disk data compression reduces disk I/O counts and improves data read performance.
    • Support for powerful indexes
    • Dynamic data schema, not limited by table structure;
  • Geographic location information storage, through geographic location index, can be convenient and fast to query specific location information.
  • As a cache database, using its in-memory storage engine;
  • Real-time website data processing; It is ideal for real-time inserts, updates, and queries, and has the replication and high scalability required for real-time data storage on a web site.

——end

This is the end of this article, this article just talked with you about the basic knowledge of MongoDB, the details are not detailed description, if you are interested in you can go to the information.

Finally, I plan to write a series of MongoDB articles recently. The title of the series of articles has been decided, and the title is as follows:

First hear MongoDB (a), take you to understand the past life of MongoDB from zero

(2) Database installation and use of visual tools installation

(3) familiar with MongoDB, MongoDB commonly used SQL statements and index knowledge

(4) Use Java to connect and operate MongoDB database

⑤, love MongoDB (five), one article with you to understand the actual operation of MongoDB

Like + comment + forward yo

If this article is helpful to you, please wave your love to make a fortune of the little hand under the praise ah, your support is my continuous creation of power, thank you!

You can VX search [Muzi lei] public number, adhere to high quality original Java technology articles, a lot of welfare yo!

And if you want to see the series of subsequent articles, please give a lot of likes and comments ah, your support is my motivation to continue to create!

Review previous articles

Query the execution process of SQL

< span style = “font-size: 14px

③ The average response time of online service is too long, how to check?

The database connection pool is exhausted, resulting in no response to Tomcat requests and a state of suspended animation

References:

(1), the Chinese version of directing a Chinese manual | official document

② MongoDB introduction

MongoDB storage engine (middle) — WiredTiger