热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP与MongoDB数据库开发

【摘要】MattButcher发表在自己Blog上的关于PHP开发者应该知道的MongoDB的5件事儿。原文题目《MongoDB:5ThingsEveryPHPDeveloperShouldKnowAboutMongoDB》,在豆瓣上有相应的翻译《MongoDB:PHP开发者应该知道的关于MongoDB的5件事儿》【主

【摘要】

      Matt Butcher 发表在自己Blog上的关于PHP开发者应该知道的MongoDB的5件事儿。原文题目《MongoDB: 5 Things Every PHP Developer Should Know About MongoDB》,在豆瓣上有相应的翻译《MongoDB:PHP开发者应该知道的关于 MongoDB的5件事儿》

【主题】

2010 SQL died ,the year of the document database.

MongoDB is a stand-alone server

It is document based,not table-based

It is schemaless

You don't need to learn another query language

It has great PHP support

【内容】

     2010 will be remembered as the year SQL died; the year relational databases were moved off of the front line; the year that developers discovered that they no longer had to force every single object into a tabular structure in order to persist the data.

     2010 is the year of the document database. While momentum has been steadily building over the last seven years or so, there are now a wide variety of stable document databases -- from cloud-based ones from Amazon and Google, to a wide variety of Open Source tools, most notably CouchDB and MongoDB.

     So what is MongoDB? Here are five things every PHP developer should know about it:

MongoDB is a stand-alone server

It is document based, not table-based

It is schemaless

You don't need to learn another query language

It has great PHP support

     Read on to learn a little about each of these.

1. MongoDB is a stand-alone server

     Like MySQL or other PostgreSQL, MongoDB listens on a port for incomming connections. It provides tools for querying, creating, updating, and deleting. In theory, you work with it in much the same way that you work with MySQL or PostgreSQL: Connect, perform operations, and close the connection.

2. Goodbye rows and tables, hello documents and collections

     Instead of storing data as rows in tables, MongoDB stores entire documents. If a have an piece of 'article' data with a title, multiple authors, a body, and tags, the entry is basically going to look like this:

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

     The key thing to notice about the example above is that this one record -- this document -- is actually stored like a document, and supports things like multiple values for the same field. There is no need to normalize the data into separate tables because, well, there are no tables.

Now, instead of storing documents in a table, they are stored in a collection, which you might think of as something akin to a big list of documents.

3. MongoDB is schemaless

     There is no schema language for MongoDB. If you want to create a new document type, you needn't do anything to tell the database about it beyond simply pushing the new data into the database.

     In number2, I mocked up a document. Now if I wanted to define an article type with those fields on it, all I need to do is write the object into the database. What if I decide later to add, say, a date? I just pull the article out of the database, add the date field, and save it.

     What about data types? The short answer is that MongoDB uses a type coercion system similar to Javascript or PHP. That is, the database is effectively weakly typed.

There are a few caveats to that (large chunks of data may need some explicit handling), but for the most part, you can write your MongoDB code like your PHP code.

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

     Remember all of those database abstraction layers you've written? Remember all of those ORM layers you've worked with? Well you can toss them out. You don't need them with Mongo.

     MongoDB (when using it's PHP driver) doesn't have much of a query language. In most cases, you simply give it an array specifying information you want, and it returns you an array of documents.

     If you want to run some really sophisticated queries (like Map-Reduce operations), you can pass Javascript functions into MongoDB, and its internal Javascript engine can execute the script.

5. PHP and MongoDB are a match made in heaven

     PHP already has great MongoDB support. The Mongo driver is available as a PECL extension to PHP, which means that installing it is as easy as running pecl install mongo. (Earlier I wrote a short article detailing the installation process)

     From there, you can get started working with Mongo's API. Complexity-wise, it ranks alongside PDO. It's not dead-simple, but there should be nothing foreign about it for anyone who has done database development before.

     The API documentation (linked above) includes a tutorial and lots of samples, so you should be able to ramp up in a very short period of time. And here's what you will notice as you go:

MongoDB is blazingly fast

Development time is faster, too, since there are no schemas to manage, and very little (if any) data mapping.

Learning curves are short, because there is no new query language to learn.

Code is trimmer. After all, you don't need another ORM, and wrappers are likely to be thin.

Your code is future-proof. It's trivially easy to add more fields -- even complex fields -- to your objects. So as requirements change, you can adapt code quickly.

     It took me only one session of MongoDB programming to realize its game-changing potential. That's what leads me to claim that the new generation of document databases are going to leave SQL-based RDBM systems in the dust -- or, more likely, leave them to do what they do well: store data that actually belongs in rows and tables.


推荐阅读
  • PHP面试题精选及答案解析
    本文精选了新浪PHP笔试题及最新的PHP面试题,并提供了详细的答案解析,帮助求职者更好地准备PHP相关的面试。 ... [详细]
  • 解决ADODB连接Access时出现80004005错误的方法
    本文详细介绍了如何解决在使用ADODB连接Access数据库时遇到的80004005错误,包括错误原因分析和具体的解决步骤。 ... [详细]
  • 本文详细解析了MySQL中常见的几种错误,并提供了具体的解决方法,帮助开发者快速定位和解决问题。 ... [详细]
  • 本文探讨了如何在PHP与MySQL环境中实现高效的分页查询,包括基本的分页实现、性能优化技巧以及高级的分页策略。 ... [详细]
  • 本文介绍了如何通过安装 sqlacodegen 和 pymysql 来根据现有的 MySQL 数据库自动生成 ORM 的模型文件(model.py)。此方法适用于需要快速搭建项目模型层的情况。 ... [详细]
  • H5技术实现经典游戏《贪吃蛇》
    本文将分享一个使用HTML5技术实现的经典小游戏——《贪吃蛇》。通过H5技术,我们将探讨如何构建这款游戏的两种主要玩法:积分闯关和无尽模式。 ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • CRZ.im:一款极简的网址缩短服务及其安装指南
    本文介绍了一款名为CRZ.im的极简网址缩短服务,该服务采用PHP和SQLite开发,体积小巧,约10KB。本文还提供了详细的安装步骤,包括环境配置、域名解析及Nginx伪静态设置。 ... [详细]
  • 解决JavaScript中法语字符排序问题
    在开发一个使用JavaScript、HTML和CSS的Web应用时,遇到从SQLite数据库中提取的法语词汇排序不正确的问题,特别是带重音符号的字母未按预期排序。 ... [详细]
  • 如何在U8系统中连接服务器并获取数据
    本文介绍了如何在U8系统中通过不同的方法连接服务器并获取数据,包括使用MySQL客户端连接实例的方法,如非SSL连接和SSL连接,并提供了详细的步骤和注意事项。 ... [详细]
  • Redis:缓存与内存数据库详解
    本文介绍了数据库的基本分类,重点探讨了关系型与非关系型数据库的区别,并详细解析了Redis作为非关系型数据库的特点、工作模式、优点及持久化机制。 ... [详细]
  • 第一步java代码条件匹配与之对应的mongo数据查询第二步:java代码分组查询与之所对应的mongodb中sheel与所得出的表点击某个_id字段进入,所得出的图表为第三步:在 ... [详细]
  • MongoDB核心概念详解
    本文介绍了NoSQL数据库的概念及其应用场景,重点解析了MongoDB的基本特性、数据结构以及常用操作。MongoDB是一个高性能、高可用且易于扩展的文档数据库系统。 ... [详细]
  • 在Android应用开发过程中,开发者经常遇到诸如CPU使用率过高、内存泄漏等问题。本文将介绍几种常用的命令及其应用场景,帮助开发者有效定位并解决问题。 ... [详细]
  • 如何在Django框架中实现对象关系映射(ORM)
    本文介绍了Django框架中对象关系映射(ORM)的实现方式,通过ORM,开发者可以通过定义模型类来间接操作数据库表,从而简化数据库操作流程,提高开发效率。 ... [详细]
author-avatar
mobiledu2502931997
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有