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 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("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 database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
	
   $document = array( 
      "title" => "MongoDB", 
      "description" => "database", 
      "likes" => 100,
      "url" => "https://tutorialspoint.tw/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->mycol;
   echo "Collection selected succsessfully";
   $cursor = $collection->find();
   // iterate cursor to display title of documents
	
   foreach ($cursor as $document) {
      echo $document["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 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 Tutorial")));
   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"] . "\n";
   }
?>

程式執行後,將產生以下結果:

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

刪除文件

要刪除文件,您需要使用 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->mycol;
   echo "Collection selected succsessfully";
   
   // now remove the document
   $collection->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 as $document) {
      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() 等的工作方式與上述相同。

廣告