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

PHP实现对MongoDB的基础操作

WBOY
發布: 2016-06-06 19:42:26
原創
1068 人瀏覽過

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板