PHP连接MongoDB操作
来源:互联网 发布:神机妙算软件安装流程 编辑:程序博客网 时间:2024/06/05 08:25
要使用PHP与MongoDB交互存储数据,需要使用MongoDB PHP驱动程序(http://pecl.php.net/package/mongo)。 从url下载驱动程序下载PHP驱动程序并确保下载的是正确的版本(如在本示例中:Win10 64位下载的版本是:php_mongo-1.6.8-5.6-ts-vc11-x64.zip
)。 现在解压缩存档并将php_mongo.dll
放入PHP扩展目录(默认为“ext
”),并将以下行添加到php.ini
文件中 -
extension = php_mongo.dll
然后重新启动 Apache 服务器,查看 phpinfo()
函数的输出结果 -
进行连接并选择数据库
要进行连接,需要指定数据库名称,如果数据库不存在,那么MongoDB会自动创建它。
以下是连接到数据库的代码片段 -
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully<br/>"; // select a database $db = $m->mydb; echo "Database mydb selected";?>
执行程序时,会产生以下结果 -
Connection to database successfullyDatabase mydb selected
创建一个集合
以下是创建集合的代码片段 -
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->createCollection("phpcol"); echo "Collection created succsessfully";?>
执行程序时,会产生以下结果 -
Connection to database successfullyDatabase mydb selectedCollection created succsessfully
插入文档
要将文档插入到MongoDB中,请使用insert()
方法。
以下是插入文档的代码片段 -
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->phpcol; echo "Collection selected succsessfully"; $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.yiibai.com/mongodb/", "by", "tutorials point" ); $collection->insert($document); echo "Document inserted successfully";?>
执行程序时,会产生以下结果 -
Connection to database successfullyDatabase mydb selectedCollection selected succsessfullyDocument inserted successfully
查找所有文件
要从集合中选择所有文档,请使用find()
方法。
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->phpcol; echo "Collection selected succsessfully"; $cursor = $collection->find(); // iterate cursor to display title of documents foreach ($cursor as $document) { echo $document["title"]. ', URL is=> ' .$document["url"] . ""; }?>
以下是选择所有文档的代码片段 -
Connection to database successfullyDatabase mydb selectedCollection selected succsessfully MongoDB, URL is=> http://www.yiibai.com/mongodb/
更新文档
要更新文档,需要使用update()
方法。
在下面的例子中,将把插入的文档的标题更新为:MongoDB教程 。 以下是更新文档的代码段 -
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; // now update the document $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB教程"))); echo "Document updated successfully"; // now display the updated document $cursor = $collection->find(); // iterate cursor to display title of documents echo "Updated document"; foreach ($cursor as $document) { echo $document["title"] .', URL => '.$document["url"] . ""; }?>
以下是选择所有文档的代码片段 -
Connection to database successfullyDatabase mydb selectedCollection selected succsessfully MongoDB, URL is=> http://www.yiibai.com/mongodb/
删除文档
要删除文档,可使用remove()
方法。
在下面的示例中,将删除标题为:MongoDB教程 的文档。 以下是删除文档的代码片段 -
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->phpcol; echo "Collection selected succsessfully"; // now remove the document $collection->remove(array("title"=>"MongoDB教程"),false); echo "Documents deleted successfully"; // now display the available documents $cursor = $collection->find(); foreach ($cursor as $document) { echo $document["title"] . ""; }?>
以下是选择所有文档的代码片段 -
Connection to database successfullyDatabase mydb selectedCollection selected succsessfully Documents deleted successfully
在上面的例子中,第二个参数是布尔类型,用于remove()
方法的justOne
字段。
其余的MongoDB方法findOne()
,save()
,limit()
,skip()
,sort()
等与上述相同。
由于此篇教程文章是一个入门级的教程文章,只是讲解有关简单入门的操作,有关更高级的内容,请参考:http://docs.mongodb.com/php-library/
- PHP连接MongoDB操作
- php操作MongoDB基础教程(连接、新增、修改、删除、查询)
- php连接MongoDB数据库及CRUD操作详解
- php操作MongoDB基础教程(连接、新增、修改、删除、查询)
- PHP连接MongoDB示例
- MongoDB连接测试操作
- mongodb连接操作
- Java连接,操作MongoDB
- PHP操作MongoDB
- PHP操作MongoDB
- PHP操作MongoDB
- PHP操作MongoDB
- PHP 操作mongodb
- php 操作mongodb(2)
- PHP 对 MongoDB 操作
- MongoDB之php操作
- PHP操作MongoDB技術總結
- php 操作mongodb
- ZendStudio自定义代码不全,自定义代码段
- thinkPHP压缩文件夹下的多个文件并下载
- Struts2、Spring、Hibernate的添加
- 分享一份自己写的关于ssh框架自动生成Bean的源代码和思路
- springmvc的执行流程详解
- PHP连接MongoDB操作
- java实训第二天7/27
- 关于基于git分支开发流程的一点看法
- LeetCode 349 Intersection of Two Arrays
- c#关闭窗体时发生的事件,关闭窗体怎样出现确认取消弹框
- CSU-ACM2017暑期训练3 J
- 二叉树已知先序中序求后序,已知后序中序求先序
- 基于UDP的网络群聊系统
- 深入浅出WPF之Binding的使用(二)