MongoDB 与PHP一起使用

MongoDB 与PHP一起使用

要将MongoDB与PHP一起使用,您需要使用MongoDB PHP驱动程序。从以下网址下载驱动程序 下载PHP驱动程序 。确保下载最新版本的驱动程序。现在解压缩存档并将php_mongo.dll放入您的PHP扩展目录(默认为“ext”),然后在您的php.ini文件中添加以下行−

extension = php_mongo.dll

建立连接并选择数据库

为了建立连接,您需要指定数据库名称,如果数据库不存在,则MongoDB会自动创建它。

下面是连接到数据库的代码片段-

<?php
   // connect to mongodb
   m = new MongoClient();

   echo "Connection to database successfully";
   // select a databasedb = $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 databasedb = m->mydb;
   echo "Database mydb selected";collection = $db->createCollection("mycol");
   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 databasedb = m->mydb;
   echo "Database mydb selected";collection = db->mycol;
   echo "Collection selected succsessfully";document = array( 
      "title" => "MongoDB", 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://www.tutorialspoint.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 databasedb = m->mydb;
   echo "Database mydb selected";collection = db->mycol;
   echo "Collection selected succsessfully";cursor = collection->find();
   // iterate cursor to display title of documents

   foreach (cursor as document) {
      echodocument["title"] . "\n";
   }
?>

当程序执行时,将产生以下结果−

Connection to database successfully
Database mydb selected
Collection selected succsessfully {
   "title": "MongoDB"
}

更新文档

要更新一个文档,你需要使用update()方法。

在下面的示例中,我们将更新插入文档的标题为 MongoDB教程 。以下是更新文档的代码片段:

<?php
   // connect to mongodb
   m = new MongoClient();
   echo "Connection to database successfully";

   // select a databasedb = m->mydb;
   echo "Database mydb selected";collection = db->mycol;
   echo "Collection selected succsessfully";
   // now update the documentcollection->update(array("title"=>"MongoDB"), 
      array('set'=>array("title"=>"MongoDB Tutorial")));
   echo "Document updated successfully";

   // now display the updated documentcursor = collection->find();

   // iterate cursor to display title of documents
   echo "Updated document";

   foreach (cursor as document) {
      echodocument["title"] . "\n";
   }
?>

当程序被执行时,将会产生以下结果−

Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
   "title": "MongoDB Tutorial"
}

删除文档

要删除文档,您需要使用remove()方法。

在以下示例中,我们将删除标题为 MongoDB Tutorial 的文档。以下是删除文档的示例代码段:

<?php
   // connect to mongodb
   m = new MongoClient();
   echo "Connection to database successfully";

   // select a databasedb = m->mydb;
   echo "Database mydb selected";collection = db->mycol;
   echo "Collection selected succsessfully";

   // now remove the documentcollection->remove(array("title"=>"MongoDB Tutorial"),false);
   echo "Documents deleted successfully";

   // now display the available documents
   cursor =collection->find();

   // iterate cursor to display title of documents
   echo "Updated document";

   foreach (cursor asdocument) {
      echo $document["title"] . "\n";
   }
?>

当程序执行时,将产生以下结果:

Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully

在上面的示例中,第二个参数是布尔类型,用于 remove() 方法的 justOne 字段。

其余的MongoDB方法: findOne()、save()、limit()、skip()、sort() 等,都与上面解释的一样。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程