1024programmer Java Ditch the relational database Five things PHP programmers should know about MongoDB

Ditch the relational database Five things PHP programmers should know about MongoDB

2010 should be remembered as the year 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, now is the emergence of more and more extensive document databases

The year 2010 should be remembered as the year 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 beginning 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 PHP developers should know:

1. MongoDB is a separate server;

2. MongoDB is based on documents, not tables;

 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

<?php
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, supporting 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 fewer schemas

MongoDB does not have a 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 need to pull out this part of the data, add the date field, and finally save it.

What about data types? The short answer is that MongoDB uses an enforcement 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�Started 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 get up and running in no time. Below are some very useful tips for you.

MongoDB is developing very fast.

Development time is very short, there are not a lot of schemas to manage, and there is little (if any) data mapping.

Since 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, or 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 tags in JSON format to easily query objects and arrays embedded in the document.

 ◆ Complete index support: including document embedded 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).

 ◆ Automatic sharding to support cloud-level scalability (in early alpha stage): The automatic sharding feature supports horizontal database clusters and can dynamically add additional machines.

The main goal of MongoDB is to build a bridge between key/value storage (which provides high performance and high scalability) and traditional RDBMS systems (rich functions), combining 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 of the website.

 ◆ 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 composed 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, the use of MongoDB will also have some limitations, 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 for specific problems will produce 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.

MongoDB: 5 Things Every PHP Developer Should Know About MongoDB

Translation: 51CTO Peng Fan

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/ditch-the-relational-database-five-things-php-programmers-should-know-about-mongodb/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索