Home > Backend Development > PHP Tutorial > Five Things PHP Programmers Should Know About MongoDB_PHP Tutorial

Five Things PHP Programmers Should Know About MongoDB_PHP Tutorial

WBOY
Release: 2016-07-13 17:42:58
Original
783 people have browsed it

 2010年应该被人们记住,因为SQL将在这一年死去。这一年关系数据库行将就木,这一年开发者发现他们再不需要长时间辛苦的构造列或者表格来存放数据。

  2010年将是文档型数据库的起始年。尽管这样的势头已经持续多年,现在才是一个更多,更广泛的文档型数据库出现的年代。从基于云计算的Amazon到Google,大量开源工具,以及随之诞生的CouchDB和MongoDB。

  那么什么是MongoDB?下面有五件事是PHP开发者应该了解的:

  1. MongoDB是一个单独的服务器;

  2. MongoDB是基于文档,而不是基于表;

  3. MongoDB中较少的Schema;

  4. 你不需要学习另外的语言;

  5. MongoDB有良好的PHP支持。

  1. MongoDB是一个单独的服务器

  就像MySQL和PostgreSQL一样,MongoDB将监听接入的链接。它提供的工具作用包括询问,创建,更新和删除。理论上,你将跟在MySQL和PostgreSQL一样的进行工作:链接,处理,然后再关闭链接。

  2. 向行和表说再见,欢迎文档和收集器

  代替储存数据的表和行,MongoDB将数据储存在文档中。假如我们有一篇带标题的“文章”,它有多个作者,一个主题和标签。所有这些看起来就像下面:

  

  array(

  title=>Hello World,

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

  body=>Hello world,

  tags=>array(tag1,tag2,tag3)

  );

  ?>

  上面的例子中最关键的就是那一条记录——这篇文档——是的,确实存储起来就像是一篇文档,支持复合形式的值存储在同一区域。不再需要结构化,不需要将数据按照表来区分。因此,表已经不存在了。

  3. MongoDB包含较少的schema

  MongoDB没有schema语言。如果你想新建一种文档类型,你不需要告诉数据库任何事情。尽管将新的数据放到数据库中就行了。

  在第二点中,我模拟了一个文档。现在我想为所有区域定义一个文章类型,所有我需要做的就是将这些数据写到数据库中。如果我决定延缓写入呢?我只需要拉出这部分数据,然后加上日期字段,最后保存就行。

  那么数据类型怎么办?简单的回答就是MongoDB运用一种强制系统,类似JavaScript或者PHP。如此这样,数据库极好的弱化了类型的作用。

  这有一些漏洞(超大量的数据需要一些明确的定义),不过多数情况下,你写你的MongoDB代码就像在PHP上编程一样。

  4. 你不需要学习另外的语言

  回忆一下其他你曾写过的数据库抽象层。回忆所有你曾使用过的ORM层。那么你现在可以抛弃他们的,在MongoDB上你用不着他们了。

  MongoDB(包含它的PHP驱动)不需要询问语言。在大多数案例中,你只需要简单的给定一个指针具体制定你需要的,然后返回你一个文档指向。

  如果你运行一些高阶函数(比如Map-Reduce),你可以通过JavaScript应用加入到MongoDB中去,并且在JavaScript内部引擎中运行这些脚本。

  5. PHP和MongoDB是天生一对?

  PHP已经对MongoDB具备很好的支持。Mongo驱动可以作为一个PECL加载项加入到PHP,这意味着安装起来就像运行PECL一样安装Mongo。

  看到这里,你可以开始编写Mongo的API了。更广一些说,它和PDO排在一起。不是简单的消亡,但是绝对不同于我们之前开发过的数据库。

  API的说明文档将包括一个引导和许多例子,这样你就可以在短时间内自举。下面将是对你十分有用的提示。

  MongoDB发展非常快。

  开发时间非常短,没有过多的模式来管理,很少(如果有的话)的数据映射。

  因为没有新的查询语言要学习,代码的调整很小。毕竟,你不需要另外的ORM,封包也非常轻。

  你的代码是未来的保证,可以更轻松的为你的对象增加更多的领域,甚至是更复杂的领域。因此你的代码可以很轻松的适应需求的变化。

  延伸阅读

  Mongo是一个高性能,开源,无模式的文档型数据库,它在许多场景下可用于替代传统的关系型数据库或键/值存储方式。Mongo使用C++开发,提供了以下功能:

  ◆ 面向集合的存储:适合存储对象及JSON形式的数据。

  ◆ 动态查询:Mongo支持丰富的查询表达式。查询指令使用JSON形式的标记,可轻易查询文档中内嵌的对象及数组。

 ◆ 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 (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 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 of 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 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/486026.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...
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