Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use of MongoDB to database

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

MongoDB to database usage? I believe that many inexperienced people are helpless about this. For this reason, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Today's topic is to talk about databases from MongoDB. In daily projects, we generally use mysql as a database, but once there is a problem, we often hear voices like "try MongoDB instead." So let's talk about databases casually.

what is database

In the simplest terms, a database is a repository of data.

Basic concepts of database

A database is a warehouse that organizes, stores, and manages data according to a certain data structure.

We write programs that run in memory, and once the program ends or the computer breaks, all the data in the program will be lost; so we need to persist some program data to the keyboard to ensure data security.

Database is the popular choice for mass data persistence, 1. File 2. database

Why use databases to store data

The database is structured.

Database can provide a variety of interfaces, so that data processing (add, delete, change) fast and convenient

Various languages (PHP, jsp,.net..) provides a perfect interface

Database Popularity (Source: https://db-engines.com/en/ranking)

From the statistical data, it can be seen that the most popular DBMS is "relational," occupying four of the top five places, expanding the data to the top ten, and relational databases also occupy seven places.

Why do most programmers prefer MySQL?

Open source, only businesses need to buy licenses

Has a wide range of uses: can be widely used on most platforms, such as Linux, Windows, Ubuntu, Mac OS X, etc.

easy to use

Reliable: tried and tested over the years

Applicable to PHP (PHP first in the world), can also be used with other programming languages, such as JAVA, PERL, C, C++, etc.

Suitable for small and large applications

So why does MongoDB still appear in the top five? Let's take a look.

What are the disadvantages of relational databases and MySQL?

Scalability: Adding more data to a particular record may involve scaling to multiple tables, columns, and rows, and because data is stored in rows, relational databases read entire rows of data from storage devices into memory even if they operate on only one of the columns, resulting in higher I/O

One defect that results from the first is the inability to store data structures

Speed: Due to the problem of data structure, it takes a certain amount of time to analyze the data. In addition, only substring matching queries can be performed. When the data in the table gradually increases, the matching of like queries will be very slow, even if there is an index.

Before use, you need to write a schema to define the table. At the same time, it is inconvenient to expand the table structure schema. If you need to modify the table structure, you need to execute DDL(data definition language). The statement modification will lead to locking the table during modification.

What makes MongoDB so attractive?

Flexibility: Document structure is more consistent with how developers code in their respective programming languages, which are clear and structured in key-value pairs, making it easy to add and edit data/documents at any time

Support various queries: fields, expressions, range queries, JavaScript functions, etc.

Faster turnaround time: Because related data stored in multiple tables in MySQL database is stored in the same document in MongoDB

There is no strict schema: documents can be created before their structure is defined

MongoDB's features make it better suited for processing large amounts of data

Let's compare the two and explain in detail

MySQLMongoDB version 1995- 2018 (mysql 8.0) 2009 Structured Relational Non-Relational Weak flexibility, need to define database schema before use Considerable flexibility compared to MySQL-define unwanted schema Extensibility Yes, but more difficult, MySQL database can scale vertically, can add more resources to a single server More scalable than MySQL. MongoDB is horizontally scalable and can add more servers to scale your database. DB admin is needed. Both developers and administrators can use it for scenarios such as accounting firms and banks, and other companies that need structured data with a clear architecture. Ideal for businesses with more or less fixed requirements (twitter exception) Ideal for businesses with real-time data, IoT, content management, mobile apps, Social networks, systems for big data/web analytics, and structured data that don't need to have a clear architecture or its architecture is constantly changing

flexibility

First of all, flexibility. For example, there will be many goods in a mall, and these goods have their own unique attributes, such as TV has screen size and screen resolution, while air conditioner has cooling type, external noise and other attributes. It is very difficult to put them in the product list, adding extra programmers to the work of data table design, while MongoDB will be very simple without Schema (schema, data model).

MongoDB's flexibility also applies to unstructured and semi-structured data. MongoDB provides full-text indexing and also supports geolocation queries and indexing. For example, if a user wants to know where there is a public restroom within a five-kilometer radius, this is a "geographical range query." Then he searches for the nearest bike. Mobike uses MongoDB to complete such "distance sorting queries."

scalability of

Data can't fit on one machine, so you need to sharding it onto several machines. Fragmentation has been a native feature of MongoDB for many years and is efficiently integrated with other MongoDB features.

For example, a complex aggregate query in a sharded cluster is automatically distributed to run on multiple nodes based on the Shard Key, pushing the computation down to the data nodes as much as possible, and finally aggregating the results of all nodes on a single node. Shards can also automatically migrate data between nodes, balancing their data volumes. At the same time, with MongoDB replication (replica set) technology, it can effectively avoid data loss (when testing, mongo will automatically discover all machine addresses of replica set, when a Mongo is stopped, the connected Server will not report error)

Using sharded cluster structure in MongoDB Distribution:

Disadvantages of MongoDB

MongoDB is known to consume a lot of server memory.

MongoDB is slightly less secure.

Data errors caused by too free and flexible file storage formats (......)

Single document size limited to 16 MB

Not rich enough for array-type data operations

When to Choose MongoDB

Having said all this shit, the most important thing is when we choose to use MongoDB.

Log system, the log information generated in the process of system operation, generally more types, larger scope, content is also relatively messy. MongoDB can collect and manage these messy logs

Geographic location storage, MongoDB supports geographic location, two-dimensional spatial index, can store latitude and longitude, so you can quickly calculate the distance between two points, such as location information

The data scale is growing rapidly (e.g., the focus information provided)

Need to ensure high availability environment

file storage demands

Other scenarios, such as game development, can store user information, equipment, points, etc. through MongoDB, in addition to logistics systems, social systems, and even Internet of Things systems

Type of database

Having said that, why do we compare mysql and MongoDB together for selection, because they are different types of databases, from database development to date, roughly divided into three types

RDBMS (Relational Database)

The first thing to mention must be the relational database to which we are most familiar with mysql database belongs.

Characteristics of relational databases:

For example, MySQL, SQL Server Oracle, etc.

Features are linked by tables

SQL is used to manage databases.

Nosql (non-relational database)

NoSql, which is the database type of MongoDB, originated from a Meetup held in San Francisco in 2009, where NoSql technology description appeared: open source, distributed, non relational databases

Features of non-relational databases:

There is no concept of rows and columns. Store data in json class

Sets are equivalent to "tables" and documents to "rows"

Standardized and non-standardized friction.

Standardization restricts innovation, non-standard words cannot be unified

NoSql was interpreted as Non-Relational and No-SQL when it was first proposed. However, with the rapid development in recent years, SQL has been gradually applied to a wider range of fields. Therefore, SQL is no longer an exclusive feature of RDBMS. SQL capabilities have also been introduced into the NoSql technology system, thus evolving the concept of Not-Only-SQL.

Most NoSql technologies weaken the support for ACID semantics and complex associative queries, adopt a simpler or more professional data model, optimize the read and write path, and thus can exchange for higher read and write performance.

NewSql

According to the definition in Wiki

NewSQL is a class of modern relational database management systems that seek to provide the same scalable performance of NoSQL systems for online transaction processing (OLTP) read-write workloads while still maintaining the ACID guarantees of a traditional database system.

NewSql can be said to be the product of the combination of traditional RDBMS and NoSql technology. Therefore, typical NewSql technology can be understood as a distributed relational database, and it is a basic premise to support distributed transactions. NoSQL and NewSQL have a lot of overlap in the technology stack, but there are obvious differences in whether to support relational models and support complex transactions. Because I do not understand, so do not say more here.

Here I just briefly introduce the type of database, for a storage technology belongs to NoSql or NewSql, or RDBMS can not be simply classified, after all, technology is constantly improving, such as MySQL is now compatible with nosql features:

Or some people wonder why transactions are not mentioned in the introduction of MongoDB's shortcomings. This is because MongoDB introduced transactions in the summer of 2018. It supports multi-document ACID features, such as using mongo shell for transaction operations.

Specific pressure measurement data will be supplemented later

After reading the above, do you know how to use MongoDB to the database? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report