MongoDB learning journey 3: MongoDB Architecture

Source: Internet
Author: User
Tags comparison table install mongodb mongodb server relational database table

All content involving internal structures is abstract, but the structure of things often determines the nature of things. If you want to know mongodb in a more fundamental way, take it easy!

Let's talk nonsense and step into the topic:

MongoDB is a portable database that can be used on every popular platform, the so-called cross-platform feature.

A running MongoDB database can be considered as a MongoDB Server, which consists of instances and databases, generally, the MongoDB Server contains one instance and multiple corresponding databases. However, in special circumstances, such as limited hardware investment or special application requirements, it also allows a Server to have multiple instances and multiple databases.

A collection of physical files (data files, log files, etc.) in MongoDB or the corresponding logical structure (collection, documents, etc.) is called a database. In short, A database is composed of a series of physical files related to disks.

The architecture is divided into logical structure and storage structure.

The logical structure is hard to understand.

The logical structure of MongoDB is a hierarchical structure. It consists of three parts: document, collection, and database. The logical structure is user-oriented. Users who use MongoDB to develop applications use the logical structure.

The document of MongoDB is equivalent to a row of records in a relational database.
Multiple documents form a collection, which is equivalent to a relational database table.
Multiple collections are logically organized together, that is, databases ).
One MongoDB instance supports multiple databases ).

A very vivid comparison table between mongodb and relational databases:

Storage structure, which is both a key and a difficult part, because it is very abstract!

The default data directory of MongoDB is/usr/local/mongodb/data, which stores all MongoDB data files. In MongoDB, each database contains a. ns file and some data files, and these data files will become more and more as the data volume increases. So if there is a database named foo in the system, the files that constitute the foo database will be composed of foo. ns, foo.0, foo.1, foo.2. You can open the database folder when you install mongodb, which contains these files. When you create a new database, a group of data files named after the database will be generated, I have also tried it. foo.2 is twice the size of foo.1 and foo.1 is twice the size of foo.0.

MongoDB has a pre-allocated space mechanism. Each pre-allocated file is filled with 0. Because of this mechanism, MongoDB always maintains extra space and spare data files, this effectively avoids the disk pressure caused by the Data surge.

As the amount of data in the table increases, the size of each data file is twice the size of the previous data file, and the maximum size of each data file is 2 GB. This mechanism can prevent small databases from wasting too much disk space and ensure that large databases have reserved space.

Each table in the database corresponds to a namespace, and each index also has a corresponding namespace. The metadata of these namespaces is concentrated in the *. ns file.

In, the foo database contains three files used to store table and index data. The foo.2 file is a pre-allocated empty file. Foo.0 and foo.1 data files are divided into different namespaces for the corresponding disk areas.


Shows the relationship between the namespace and the disk area. Each namespace can contain multiple different disk areas, which are not consecutive. As data files grow, the size of the Disk Area corresponding to each namespace increases with the number of times allocated. The purpose is to balance the space wasted by the namespace and maintain data continuity in a namespace. There is also a namespace to note: $ freelist, which is used to record the disk areas that are no longer in use (the deleted Collection or index ). Whenever a namespace needs to be allocated to a new disk area, you will first check whether $ freelist has a suitable Disk Area for use, so that free disk space is reclaimed.

Of course, this article is based on my own summary of the materials. I have read a little dizzy, but I have been using mongodb for a while. I will read this article again, this is much easier to understand than the first time.

So I hope that beginners can learn and use it while learning it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.