【转】PHP操纵MongoDB【NoSQL】
添加时间:2013-6-21 点击量:
原文:http://blog.sina.com.cn/s/blog_4b67d3240101519b.html
一、MongoDB简介
MongoDB (名称来自humongous) 是一个可扩大的、高机能、开源、模式、面向文档的数据库,集文档数据库、键值对存储和关系型数据库的长处于一身。官方站点:http://www.mongodb.org/,MongoDB特点:
•面向文档存储(类JSON数据模式简单而强大)
•动态查询
•全索引支撑,扩大到内部对象和内嵌数组
•查询记录解析
•快速,当场更新
•高效存储二进制大对象 (比如照片和视频)
•复制和故障切换支撑
•Auto-Sharding主动分片支撑云级扩大性
•MapReduce 支撑错杂聚合
•贸易支撑,培训和咨询
二、安装MongoDB
安装MongoDB很是的简单,仅需紧缩包解压运行号令即可,地址:http://www.mongodb.org/downloads,本文为windows平台,MongoDB运行号令:>bin/mongod。提示:起首要创建存储数据的文件夹,MongoDB 默认存储数据目次为 /data/db/ (或者 c:\data\db),当然你也可以批改成不合目次,只须要指定 --dbpath 参数,eg:
>bin/mongod --dbpath=d:\mgdata\db
三、安装MongoDB PHP扩大
按照本身的PHP版本PHP扩大:http://github.com/mongodb/mongo-php-driver/downloads,提示:
1、VC适Apache、VC9合适IIS;
2、Thread safe合适PHP以模块运行体式格式、Non-thread safe合适CGI运行体式格式。
批改php.ini,参加:extension=php_mongo.dll,重启Web办事器。
四、PHP示例
1、连接Mongo办事器
view plaincopy to clipboardprint?
<?php
//连接localhost:27017
¥conn = new Mongo();
//连接长途主机默认端口
¥conn = new Mongo(test.com);
//连接长途主机22011端口
¥conn = new Mongo(test.com:22011);
//MongoDB有效户名暗码
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost)
//MongoDB有效户名暗码并指定命据库blog
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost/blog);
//多个办事器
¥conn = new Mongo(mongodb://localhost:27017,localhost:27018);
?>
<?php
//连接localhost:27017
¥conn = new Mongo();
//连接长途主机默认端口
¥conn = new Mongo(test.com);
//连接长途主机22011端口
¥conn = new Mongo(test.com:22011);
//MongoDB有效户名暗码
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost)
//MongoDB有效户名暗码并指定命据库blog
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost/blog);
//多个办事器
¥conn = new Mongo(mongodb://localhost:27017,localhost:27018);
?>
2、指定命据库和数据集名(表名)
view plaincopy to clipboardprint?
<?php
//选择数据库blog
¥db = ¥conn->blog;
//制订成果集(表名:users)
¥collection = ¥db->users;
?>
<?php
//选择数据库blog
¥db = ¥conn->blog;
//制订成果集(表名:users)
¥collection = ¥db->users;
?>
3、CRUD
view plaincopy to clipboardprint?
<?php
//新增
¥user = array(name => caleng, email =>admin@admin.com);
¥collection->(¥user);
//批改
¥newdata = array(¥set => array(email => test@test.com));
¥collection->(array(name => caleng), ¥newdata);
//删除
¥collection->remove(array(name=>caleng), array(justOne => true));
//查找
¥cursor = ¥collection->find();
var_dump(¥cursor);
//查找一条
¥user = ¥collection->findOne(array(name => caleng), array(email));
var_dump(¥user);
?>
<?php
//新增
¥user = array(name => caleng, email =>admin@admin.com);
¥collection->(¥user);
//批改
¥newdata = array(¥set => array(email => test@test.com));
¥collection->(array(name => caleng), ¥newdata);
//删除
¥collection->remove(array(name=>caleng), array(justOne => true));
//查找
¥cursor = ¥collection->find();
var_dump(¥cursor);
//查找一条
¥user = ¥collection->findOne(array(name => caleng), array(email));
var_dump(¥user);
?>
4、封闭连接
view plaincopy to clipboardprint?
<?php
¥conn->close();
?>
我们永远不要期待别人的拯救,只有自己才能升华自己。自己已准备好了多少容量,方能吸引对等的人与我们相遇,否则再美好的人出现、再动人的事情降临身边,我们也没有能量去理解与珍惜,终将擦肩而过。—— 姚谦《品味》
原文:http://blog.sina.com.cn/s/blog_4b67d3240101519b.html
一、MongoDB简介
MongoDB (名称来自humongous) 是一个可扩大的、高机能、开源、模式、面向文档的数据库,集文档数据库、键值对存储和关系型数据库的长处于一身。官方站点:http://www.mongodb.org/,MongoDB特点:
•面向文档存储(类JSON数据模式简单而强大)
•动态查询
•全索引支撑,扩大到内部对象和内嵌数组
•查询记录解析
•快速,当场更新
•高效存储二进制大对象 (比如照片和视频)
•复制和故障切换支撑
•Auto-Sharding主动分片支撑云级扩大性
•MapReduce 支撑错杂聚合
•贸易支撑,培训和咨询
二、安装MongoDB
安装MongoDB很是的简单,仅需紧缩包解压运行号令即可,地址:http://www.mongodb.org/downloads,本文为windows平台,MongoDB运行号令:>bin/mongod。提示:起首要创建存储数据的文件夹,MongoDB 默认存储数据目次为 /data/db/ (或者 c:\data\db),当然你也可以批改成不合目次,只须要指定 --dbpath 参数,eg:
>bin/mongod --dbpath=d:\mgdata\db
三、安装MongoDB PHP扩大
按照本身的PHP版本PHP扩大:http://github.com/mongodb/mongo-php-driver/downloads,提示:
1、VC适Apache、VC9合适IIS;
2、Thread safe合适PHP以模块运行体式格式、Non-thread safe合适CGI运行体式格式。
批改php.ini,参加:extension=php_mongo.dll,重启Web办事器。
四、PHP示例
1、连接Mongo办事器
view plaincopy to clipboardprint?
<?php
//连接localhost:27017
¥conn = new Mongo();
//连接长途主机默认端口
¥conn = new Mongo(test.com);
//连接长途主机22011端口
¥conn = new Mongo(test.com:22011);
//MongoDB有效户名暗码
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost)
//MongoDB有效户名暗码并指定命据库blog
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost/blog);
//多个办事器
¥conn = new Mongo(mongodb://localhost:27017,localhost:27018);
?>
<?php
//连接localhost:27017
¥conn = new Mongo();
//连接长途主机默认端口
¥conn = new Mongo(test.com);
//连接长途主机22011端口
¥conn = new Mongo(test.com:22011);
//MongoDB有效户名暗码
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost)
//MongoDB有效户名暗码并指定命据库blog
¥conn = new Mongo(mongodb://¥{username}:¥{password}@localhost/blog);
//多个办事器
¥conn = new Mongo(mongodb://localhost:27017,localhost:27018);
?>
2、指定命据库和数据集名(表名)
view plaincopy to clipboardprint?
<?php
//选择数据库blog
¥db = ¥conn->blog;
//制订成果集(表名:users)
¥collection = ¥db->users;
?>
<?php
//选择数据库blog
¥db = ¥conn->blog;
//制订成果集(表名:users)
¥collection = ¥db->users;
?>
3、CRUD
view plaincopy to clipboardprint?
<?php
//新增
¥user = array(name => caleng, email =>admin@admin.com);
¥collection->(¥user);
//批改
¥newdata = array(¥set => array(email => test@test.com));
¥collection->(array(name => caleng), ¥newdata);
//删除
¥collection->remove(array(name=>caleng), array(justOne => true));
//查找
¥cursor = ¥collection->find();
var_dump(¥cursor);
//查找一条
¥user = ¥collection->findOne(array(name => caleng), array(email));
var_dump(¥user);
?>
<?php
//新增
¥user = array(name => caleng, email =>admin@admin.com);
¥collection->(¥user);
//批改
¥newdata = array(¥set => array(email => test@test.com));
¥collection->(array(name => caleng), ¥newdata);
//删除
¥collection->remove(array(name=>caleng), array(justOne => true));
//查找
¥cursor = ¥collection->find();
var_dump(¥cursor);
//查找一条
¥user = ¥collection->findOne(array(name => caleng), array(email));
var_dump(¥user);
?>
4、封闭连接
view plaincopy to clipboardprint?
<?php
¥conn->close();
?>