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

WBOY
Release: 2016-07-21 15:09:50
Original
736 people have browsed it

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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template