## PHP安装MongoDB扩展驱动
window上安装 MongoDB PHP扩展:
<!--more-->
Github上已经提供了用于window平台的预编译php mongodb驱动二进制包([下载地址](https://s3.amazonaws.com/drivers.mongodb.org/php/index.html)),你可以下载与你php对应的版本,但是你需要注意以下几点问题:
1.安装的版本要与安装的PHP版本一致,例如php_mongo-1.6.8-5.4-vc9.dll运行环境是PHP5.4_ts_x86版本,且系统环境至少为vc9。
2.'Thread safe'(线程安全)是运行在Apache上以模块的PHP上,如果你以CGI的模式运行PHP,请选择非线程安全模式(' non-thread safe')。
3.下载完你需要的二进制包后,解压压缩包,将'php_mongo.dll'文件添加到你的PHP扩展目录中(ext)。ext目录通常在PHP安装目录下的ext目录。
打开php配置文件 php.ini 添加以下配置:
```
extension=php_mongo.dll
```
重启服务器。
通过浏览器访问phpinfo,如果安装成功,就会看到mongo。
## PHP操作mongodb
PHP已经提供了一系列的Mongo操作类:
http://php.net/manual/zh/book.mongo.php
`MongoClient类`
这个类用于创建和管理连接。
`MongoClient`
基本用法
```
<?php
$m = new MongoClient(); // 连接
$db = $m->foo; // 获取名称为 "foo" 的数据库
?>
```
```
Table of Contents
MongoClient::close — 关闭连接
MongoClient::connect — 连接到数据库服务器
MongoClient::__construct — 创建一个新的数据库连接对象
MongoClient::dropDB — 删除一个数据库 [已废弃]
MongoClient::__get — 取得一个数据库
MongoClient::getConnections — 返回所有已打开连接的信息
MongoClient::getHosts — 更新所有关联主机的状态信息
MongoClient::getReadPreference — 获取此连接的读取首选项
MongoClient::getWriteConcern — Get the write concern for this connection
MongoClient::killCursor — Kills a specific cursor on the server
MongoClient::listDBs — 列出所有有效数据库
MongoClient::selectCollection — 获取数据库的文档集
MongoClient::selectDB — 获取一个数据库
MongoClient::setReadPreference — 为该连接设置读取选项
MongoClient::setWriteConcern — Set the write concern for this connection
MongoClient::__toString — 该连接的字符串表达方式
```
`MongoDB类`
该类的实例用于和数据库进行交互。例如:要获取一个数据库:
```
<?php
$m = new MongoClient(); // 连接
$db = $m->selectDB("test");
?>
```
```
Table of Contents
MongoDB::authenticate — 登录到数据库
MongoDB::command — 执行一条 Mongo 指令
MongoDB::__construct — 选择一个数据库
MongoDB::createCollection — 创建一个集合
MongoDB::createDBRef — 创建数据库引用
MongoDB::drop — 丢弃数据库
MongoDB::dropCollection — Drops a collection [deprecated]
MongoDB::execute — 在数据库服务器上运行JavaScript
...
```
`MongoCollection类`
提供对集合的操作,如增删查改。
```
Table of Contents
MongoCollection::aggregate — Perform an aggregation using the aggregation framework
MongoCollection::aggregateCursor — Execute an aggregation pipeline command and retrieve results through a cursor
MongoCollection::batchInsert — Inserts multiple documents into this collection
MongoCollection::__construct — 创建一个新的集合
MongoCollection::count — 返回集合中的文档数量
MongoCollection::createDBRef — 创建一个数据库引用
MongoCollection::createIndex — Creates an index on the specified field(s) if it does not already exist.
MongoCollection::deleteIndex — Deletes an index from this collection
MongoCollection::deleteIndexes — 删除集合的所有索引
MongoCollection::distinct — 获取集合里指定键的不同值的列表。
MongoCollection::drop — 删除该集合
MongoCollection::ensureIndex — Creates an index on the specified field(s) if it does not already exist.
MongoCollection::find — 查询该集合,并返回结果集的 MongoCursor
MongoCollection::findAndModify — Update a document and return it
MongoCollection::findOne — Queries this collection, returning a single element
MongoCollection::__get — Gets a collection
MongoCollection::getDBRef — Fetches the document pointed to by a database reference
MongoCollection::getIndexInfo — Returns information about indexes on this collection
MongoCollection::getName — 返回这个集合的名称
MongoCollection::getReadPreference — Get the read preference for this collection
MongoCollection::getSlaveOkay — Get slaveOkay setting for this collection
MongoCollection::getWriteConcern — Get the write concern for this collection
MongoCollection::group — Performs an operation similar to SQL GROUP BY command
MongoCollection::insert — 插入文档到集合中
MongoCollection::parallelCollectionScan — Returns an array of cursors to iterator over a full collection in parallel
MongoCollection::remove — 从集合中删除记录
MongoCollection::save — 保存一个文档到集合
MongoCollection::setReadPreference — Set the read preference for this collection
MongoCollection::setSlaveOkay — Change slaveOkay setting for this collection
MongoCollection::setWriteConcern — Set the write concern for this database
MongoCollection::toIndexString — Converts keys specifying an index to its identifying string
MongoCollection::__toString — String representation of this collection
MongoCollection::update — Update records based on a given criteria
MongoCollection::validate — Validates this collection
```
`MongoCursor类`
用来遍历结果。例如:
```
<?php
$cursor = $collection->find();
var_dump(iterator_to_array($cursor));
?>
```
## 实战
```
<?php
$m = new MongoClient();
$db = $m->test; //选择test数据库
$c = $db->user; //选择一个集合
//find
function get($c){
$list = $c->find();
if(is_object($list))
foreach($list as $k=>$vo){
$data = array(
//'id' => $vo['_id'],
'name' => $vo['name'],
'age' => $vo['age'],
'birthday' => $vo['birthday']
);
if(empty($vo['age'])) unset($data['age']);
if(empty($vo['birthday'])) unset($data['birthday']);
$res[] = $data;
}
return $res;
}
function gets($c){
$list = $c->find();
var_dump(iterator_to_array($list));
}
$list = get($c);
//var_dump($list);
//update
$where = array('name'=>'yjc');
$new_arr = array(
'name' => 'yjc',
'age' => 25,
'birthday' => '1992-02-18'
);
//echo $res = $c->update($where, $new_arr); //全量更新,必须跟全部字段
//局部更新
$where = array('birthday'=>'1992-02-18');
$new_arr = array('$set' =>array('name' => 'yujc', 'age' => 25)); //注意用法,全部写在$set修改器数组里
//$res = $c->update($where, $new_arr);
//新增
$arr = array(
'name' => 'jiancai2',
'age' => 21,
'birthday' => '1993-02-18'
);
//$res = $c->insert($arr); var_dump($res);
//删除
$where = array('name'=>'jiancai2');
$res = $c->remove($where); var_dump($res);
//count
$user_num = $c->count();
var_dump($user_num);
//var_dump(get($c));
gets($c);
```