首页 >php教程 >php手册 >PHP实现对MongoDB的基础操作

PHP实现对MongoDB的基础操作

WBOY
WBOY原创
2016-06-06 19:42:261073浏览

PHP扩展 PHP5.2、PHP5.3,线程、非线程,IIS、Apache综合下载地址: http://pan.baidu.com/s/1sjrW6z7 下载相对应的php扩展文件php_mongo.dll,拷入php扩展文件夹里/php/ext,修改php.ini文件,新增下列行: extension=php_mongo.dll PHP 操作 连接数据库 $c

PHP扩展                                                                                     

PHP5.2、PHP5.3,线程、非线程,IIS、Apache综合下载地址:

http://pan.baidu.com/s/1sjrW6z7

下载相对应的php扩展文件php_mongo.dll,拷入php扩展文件夹里/php/ext,修改php.ini文件,新增下列行:

extension=php_mongo.dll

PHP操作                                                                                     

  • 连接数据库

<span>$conn</span> = <span>new</span> Mongo("mongodb://localhost:27017//admin:admin");
  • 选择数据库和集合

<span>//</span><span>选择数据库blog,如果没有,则创建</span>

<span>$db</span> = <span>$conn</span>-><span>yyd;

</span><span>//</span><span>也可以写成:$db = $conn->selectDB('yyd');


//制定结果集(集合:yyd_test)</span>
<span>$collection</span> = <span>$db</span>-><span>yyd_test;

</span><span>//</span><span>也可以写成:$collection = $db->selectCollection('yyd_test');
//var_dump($collection);</span>
  • 新增数据

<span>$post</span> = <span>array</span>('name' => '22', 'sex' => '32'<span>);

</span><span>$flag</span>=(<span>$collection</span>->insert(<span>$post</span><span>));
</span><span>var_dump</span>(<span>$flag</span>);

PHP实现对MongoDB的基础操作

PHP实现对MongoDB的基础操作

  • 查找数据

<span>$arr</span>=<span>array</span><span>();
</span><span>$cursor</span> = <span>$collection</span>->find(<span>$arr</span><span>);
</span><span>foreach</span>(<span>$cursor</span> <span>as</span> <span>$key</span> => <span>$value</span><span>){
    </span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>;</span>
    <span>echo</span> <span>$value</span>['_id'<span>];
    </span><span>echo</span> '<br>name:'<span>;
    </span><span>echo</span> <span>$value</span>['name'<span>];
    </span><span>echo</span> "<br>sex:"<span>;
    </span><span>echo</span> <span>$value</span>['sex'<span>];
    </span><span>echo</span> "
"; }

PHP实现对MongoDB的基础操作

  • 条件查找

<span>$arr</span>=<span>array</span>("name"=>"22"<span>);
</span><span>$cursor</span> = <span>$collection</span>->find(<span>$arr</span>);
  • 修改数据

PHP实现对MongoDB的基础操作

<span>$newdata</span> = <span>array</span>('$set' => <span>array</span>("email" => "test@test.com"<span>));
</span><span>$collection</span>->update(<span>array</span>("name" => "22"), <span>$newdata</span><span>);
</span><span>var_dump</span>(<span>$collection</span><span>);

</span><span>$arr</span>=<span>array</span>("name"=>"22"<span>);
</span><span>$cursor</span> = <span>$collection</span>->find(<span>$arr</span><span>);
</span><span>foreach</span>(<span>$cursor</span> <span>as</span> <span>$key</span> => <span>$value</span><span>){
    </span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>;</span>
    <span>echo</span> <span>$value</span>['_id'<span>];
    </span><span>echo</span> '<br>name:'<span>;
    </span><span>echo</span> <span>$value</span>['name'<span>];
    </span><span>echo</span> "<br>sex:"<span>;
    </span><span>echo</span> <span>$value</span>['sex'<span>];
    </span><span>echo</span> "<br>email:"<span>;
    </span><span>echo</span> <span>$value</span>['email'<span>];
    </span><span>echo</span> "
"; }
  • 删除数据

PHP实现对MongoDB的基础操作

<span>$arr</span>=<span>array</span>("name"=>"22"<span>);

</span><span>$collection</span>->remove(<span>$arr</span><span>);

</span><span>var_dump</span>(<span>$collection</span>);

PHP实现对MongoDB的基础操作

  • 其它常用操作

<span>//</span><span>关闭连接</span>

<span>$conn</span>-><span>close();


</span><span>//</span><span>删除一个数据库</span>

<span>$conn</span>->dropDB("yyd"<span>);


</span><span>//</span><span>列出所有可用数据库</span>

<span>$dbs</span> = <span>$conn</span>->listDBs();

 

带有MongoDB操作函数的的PHP手册:

http://pan.baidu.com/s/1pJz2llh

或者网站:

http://www.php.net/manual/zh/class.mongodb.php

 

 

 

http://www.cnblogs.com/yydcdut/p/3571430.html

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn