PHP操作MongoDB

安装MongoDB可以参照我的C#操作MongoDB那篇文章。

php的话首先从网上下载MongoDB的扩展包,https://github.com/mongodb/mongo-php-driver/downloads,选择对应的扩展包。

这是我下的,然后解压,VC6适合apache,VC9适合IIS,ts(thread safe)指PHP以模块形式运行的。

然后把其中的php_mongo.dll放在PHP中的ext文件夹中,然后在PHP.INI里面加入extension=php_mongo.dll,重启apache。

至此PHP扩展MongoDB的包安装完毕。

关于查询MongoDB一些使用函数可以查询手册http://us.php.net/manual/en/class.mongocollection.php

<pre>
<?php
error_reporting(7);

$conn = new Mongo();

$db = $conn->PHPDataBase;
$collection = $db->PHPCollection;


/*-----------------------------
* 删除
*-----------------------------
$collection->remove(array("name" => "xixi111"));
*/

/*------------------------------
* 插入
*------------------------------
for($i = 0;$i <= 50;$i++) {
$data = array("name" => "xixi".$i,"email" => "673048143_".$i."@qq.com","age" => $i*1+20);
$collection->insert($data);
}
*/

/*-------------------------------
* 查找
*-------------------------------
$res = $collection->find(array("age" => array('$gt' => 25,'$lt' => 40)),array("name" => true));

foreach($res as $v) {
print_r($v);
}
*/

/*-------------------------------
* 更新
*-------------------------------
$collection->update(array("age" =>22),array('$set' => array("name" => "demoxixi")));
*/
?>



posted @ 2011-11-14 09:46  Yolandafans  阅读(7921)  评论(0编辑  收藏  举报