PHP连接MongoDB操作

要使用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() 函数的输出结果 –

《PHP连接MongoDB操作》

进行连接并选择数据库

要进行连接,需要指定数据库名称,如果数据库不存在,那么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 successfully
Database 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 successfully
Database mydb selected
Collection 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 successfully
Database mydb selected
Collection selected succsessfully
Document 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 successfully
Database mydb selected
Collection 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 successfully
Database mydb selected
Collection 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 successfully
Database mydb selected
Collection selected succsessfully 
Documents deleted successfully

在上面的例子中,第二个参数是布尔类型,用于remove()方法的justOne字段。
其余的MongoDB方法findOne()save()limit()skip()sort()等与上述相同。

由于此篇教程文章是一个入门级的教程文章,只是讲解有关简单入门的操作,有关更高级的内容,请参考:http://docs.mongodb.com/php-library/

        原文作者: mongodb教程
        原文地址: https://www.yiibai.com/mongodb/mongodb_php.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞