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

What PHP programmers should know about MongoDB

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what PHP programmers should know about MongoDB. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

2010 should be remembered, because SQL will die in that year. This is a year when relational databases are dying, and this year developers find that they no longer need to work hard to construct columns or tables to store data.

The year 2010 will be the beginning of the documentary database. Although this momentum has been going on for many years, this is the era of the emergence of more and more documented databases. From cloud-based Amazon to Google, a large number of open source tools, and the birth of 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 access links. The tools it provides include asking, creating, updating and deleting. In theory, you will do the same work as in MySQL and PostgreSQL: link, process, and then close the link.

two。 Say goodbye to rows and tables, welcome documents and collectors

Instead of tables and rows that store data, MongoDB stores data in a document. If we have an article with a title, it has multiple authors, a topic and a tag. All of this looks like the following:

Array (

'title'= >' Hello World'

'authors'= > array ('John','Sally','Jim')

'body'= >' Hello world'

'tags'= > array ('tag1','tag2','tag3')

);

? >

The most important thing in the above example is that record-- this document-- yes, it does store like a document, supporting composite values to be stored in the same area. There is no need to be structured, and there is no need to distinguish data by table. Therefore, the table no longer exists.

3. MongoDB contains less schema

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 in the database.

In the second point, I simulated a document. Now I want to define an article type for all areas, and all I need to do is write the data to the database. What if I decide to postpone writing? I just need to pull out this part of the data, then add the date field, and finally save it.

What about the data type? The simple answer is that MongoDB uses a mandatory system, similar to JavaScript or PHP. In this way, the database weakens the role of types very well.

There are some loopholes (a huge amount of data requires some clear definition), but in most cases, you write your MongoDB code as if you were programming on PHP.

4. You don't need to learn another language.

Recall other database abstraction layers you've written about. Remember all the ORM layers you've used. Then you can abandon them now. You don't need them on MongoDB.

MongoDB (including its PHP driver) does not require a query language. In most cases, you simply give a pointer to specify what you need, and then return a document point to you.

If you run some high-order functions (such as Map-Reduce), you can add to MongoDB through the JavaScript application and run these scripts in the JavaScript internal engine.

5. Are PHP and MongoDB made for each other?

PHP already has good support for MongoDB. The Mongo driver can be added to PHP as an PECL add-on, which means that it installs Mongo just like running PECL.

Seeing here, you can start writing Mongo's API. More broadly, it is lined up with PDO. It's not a simple demise, but it's definitely different from the database we've developed before.

The documentation for API will include a guide and many examples so that you can bootstrap in a short period of time. Here are some useful tips for you.

MongoDB is developing very fast.

The development time is very short, there are not too many schemas to manage, and there are few (if any) data mappings.

Because there is no new query language to learn, the code adjustment is very small. After all, you don't need another ORM, and the packet is very light.

Your code is the guarantee of the future, making it easier to add more areas, even more complex ones, to your objects. So your code can easily adapt to changes in requirements.

Extended reading

Mongo is a high-performance, open source, schemalless 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 features:

◆ collection-oriented storage: suitable for storing objects and data in JSON form.

◆ dynamic query: Mongo supports rich query expressions. Query instructions use tags in the form of JSON to easily query objects and arrays embedded in a document.

◆ complete indexing support: including document embedded objects and arrays. Mongo's query optimizer parses the query expression 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, master-slave mode and mutual replication between servers. The primary goal of replication is to provide redundancy and automatic failover.

◆ efficient traditional storage: support for binary data and large objects (such as photos or pictures).

◆ auto-sharding to support cloud-level scalability (in the early alpha stage): the auto-sharding feature supports horizontal database clusters that 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 features), 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 insert, update and query, and has the replication and high scalability required for website real-time data storage.

◆ caching: due to its high performance, Mongo is also suitable as a cache layer for the information infrastructure. After the system restart, the persistent cache layer built by Mongo can avoid the data source overload of the lower layer.

◆ large-size, low-value data: it may be expensive to use traditional relational databases to store some data. Before that, programmers often choose traditional files for storage.

◆ 's highly scalable scenario: Mongo is ideal for databases made up of dozens or hundreds of servers. Mongo's roadmap already includes built-in support for the MapReduce engine.

◆ is used for the storage of objects and JSON data: Mongo's BSON data format is very suitable for document format storage and query.

Naturally, there are some limitations to the use of MongoDB, for example, it is not suitable:

◆ highly transactional systems: such as banking or accounting systems. At present, the traditional relational database is more suitable for applications that require a large number of atomic complex transactions.

◆ traditional business intelligence applications: BI databases for specific problems will produce highly optimized queries. For such applications, data warehouse may be a more appropriate choice.

◆ needs a problem with SQL.

MongoDB supports operating systems such as OS X, Linux and Windows, and provides drivers for Python,PHP,Ruby,Java and C++ languages, as well as drivers for Erlang and .NET platforms in the community.

This is the end of this article on "what PHP programmers should know about MongoDB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report