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

PHP实现对MongoDB的基础操作

WBOY
WBOYOriginal
2016-06-06 19:42:261074Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP操作mongodbNächster Artikel:Windows下安装PHP的MongoDB驱动