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

PHP实现对MongoDB的基础操作

WBOY
Release: 2016-06-06 19:42:26
Original
1069 people have browsed it

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");
Copy after login
  • 选择数据库和集合

<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>
Copy after login
  • 新增数据

<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>);
Copy after login

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> "
Copy after login
"; }

PHP实现对MongoDB的基础操作

  • 条件查找

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

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>
Copy after login
    <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> "
Copy after login
"; }
  • 删除数据

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>);
Copy after login

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();
Copy after login

 

带有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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template