Home Backend Development PHP Tutorial Five things you should know about MongoDB as a PHP programmer_PHP Tutorial

Five things you should know about MongoDB as a PHP programmer_PHP Tutorial

Jul 21, 2016 pm 03:09 PM
mongodb php sql learn people Will of programmer

2010 should be remembered as the year that SQL died. This is the year that relational databases are dying, and this is the year that developers find that they no longer need to laboriously construct columns or tables to store data.
2010 will be the starting year of document database. Although this momentum has been going on for many years, it is now an era when more and more extensive document-based databases are emerging. From cloud-based Amazon to Google, a large number of open source tools, and the resulting CouchDB and MongoDB.
So what is MongoDB? Here are five things that PHP developers should know:
1. MongoDB is a separate server;
2. MongoDB is based on Document, not table-based;
3. Less Schema in MongoDB;
4. You don’t need to learn another language;
5. MongoDB has good PHP support.
1. MongoDB is a separate server
Just like MySQL and PostgreSQL, MongoDB will listen for incoming connections. The tools it provides include query, create, update and delete. In theory, you will work the same as in MySQL and PostgreSQL: connect, process, and then close the connection.
2. Say goodbye to rows and tables, welcome documents and collectors
Instead of tables and rows that store data, MongoDB stores data in documents. Let's say we have a titled "article" that has multiple authors, a topic, and tags. All of these look like the following:

Copy the code The code is as follows:

array(
'title'=> ;'Hello World',
'authors'=>array('John','Sally','Jim'),
'body'=>'Hello world',
'tags' =>array('tag1','tag2','tag3')
);
?>

The most critical thing in the above example is that record - this document - yes, it is indeed stored like a document, and supports composite values ​​to be stored in the same area. There is no need to structure or separate data into tables. Therefore, the table no longer exists.
3. MongoDB contains less schema
MongoDB has no schema language. If you want to create a new document type, you don't need to tell the database anything. Just put the new data into the database.
In the second point, I simulated a document. Now I want to define an article type for all areas, all I need to do is write this data to the database. What if I decide to defer writing? I just pull out the data, add the date field, and save it.
What about data types? The simple answer is that MongoDB uses a coercion system, similar to JavaScript or PHP. In this way, the database greatly weakens the role of types.
There are some holes in this (very large amounts of data require some explicit definitions), but for the most part, you write your MongoDB code just like programming in PHP.
4. You don’t need to learn another language
Recall other database abstraction layers you have written. Recall all the ORM layers you have ever used. Then you can discard them now, you have no need for them on MongoDB.
MongoDB (including its PHP driver) does not require a query language. In most cases, you simply specify what you need given a pointer, and a document is returned to you.
If you run some higher-order functions (such as Map-Reduce), you can join MongoDB through a JavaScript application and run these scripts in the JavaScript internal engine.
5. Are PHP and MongoDB a natural match?
PHP already has good support for MongoDB. The Mongo driver can be added to PHP as a PECL add-on, which means that it is installed just like running PECL to install Mongo.
Seeing this, you can start writing Mongo’s API. More broadly, it ranks with PDO. Not simply dead, but definitely different from the databases we have developed before.
The documentation for the API will include a guide and many examples so you can be bootstrapped in no time. Below are some very useful tips for you.
MongoDB is developing very fast.
Development time is very short, there are not too many schemas to manage, and little (if any) data mapping.
Because there is no new query language to learn, the code adjustments are minimal. After all, you don't need another ORM and the packets are very lightweight.
Your code is future-proof, making it easier to add more fields, and even more complex fields, to your objects. So your code can easily adapt to changing requirements.
Further reading
Mongo is a high-performance, open source, schema-less document database that can be used to replace traditional relational databases or key/value storage in many scenarios. Mongo is developed using C++ and provides the following functions:
Collection-oriented storage: Suitable for storing objects and data in JSON form.
Dynamic query: Mongo supports rich query expressions. The query command uses JSON markup to easily query objects and arrays embedded in the document.
Complete index support: Including embedded document objects and arrays. Mongo's query optimizer analyzes query expressions and generates an efficient query plan.
Query Monitoring: Mongo includes a monitoring tool for analyzing the performance of database operations.
Replication and automatic failover: Mongo database supports data replication between servers, supports master-slave mode and mutual replication between servers. The main goal of replication is to provide redundancy and automatic failover.
Efficient traditional storage method: Supports binary data and large objects (such as photos or pictures).
Auto-sharding to support cloud-level scalability (in early alpha stage): Auto-sharding supports horizontal database clusters, with additional machines dynamically added.
The main goal of MongoDB is to build a bridge between key/value storage (providing high performance and high scalability) and traditional RDBMS systems (rich functions), integrating the advantages of both. According to the description on the official website, Mongo is suitable for the following scenarios:
Website data: Mongo is very suitable for real-time insertion, update and query, and has the replication and high scalability required for real-time data storage on the website sex.
Caching: Due to its high performance, Mongo is also suitable as a caching layer for information infrastructure. After the system is restarted, the persistent cache layer built by Mongo can prevent the underlying data source from being overloaded.
Large size, low-value data: It may be more expensive to store some data using traditional relational databases. Before this, programmers often chose traditional files for storage.
High scalability scenarios: Mongo is very suitable for databases consisting of dozens or hundreds of servers.Mongo's roadmap already includes built-in support for the MapReduce engine.
Used for storage of objects and JSON data: Mongo’s BSON data format is very suitable for storage and query in documented formats.
Naturally, there will be some limitations in the use of MongoDB, for example, it is not suitable for:
Highly transactional systems: such as banking or accounting systems. Traditional relational databases are currently more suitable for applications that require a large number of atomic and complex transactions.
Traditional business intelligence applications: BI databases targeting specific problems will generate highly optimized query methods. For such applications, a data warehouse may be a more suitable choice.
Questions that require SQL.
MongoDB supports operating systems such as OS X, Linux and Windows, and provides drivers for Python, PHP, Ruby, Java and C++ languages. The community also provides drivers for platforms such as Erlang and .NET. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327248.htmlTechArticle2010 should be remembered as the year that SQL will die. This is the year that relational databases are dying. This is the year that developers find that they no longer need to work long and hard to construct columns or tables...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles