MongoDB PHP

2022-09-06 17:13 更新

在php中使用mongodb你必須使用 mongodb的php驅(qū)動。

MongoDB PHP在各平臺上的安裝及驅(qū)動包下載請查看: PHP安裝MongoDB擴展驅(qū)動

確保連接及選擇一個數(shù)據(jù)庫

為了確保正確連接,你需要指定數(shù)據(jù)庫名,如果數(shù)據(jù)庫在mongoDB中不存在,mongoDB會自動創(chuàng)建

代碼片段如下:

<?php    // 連接到mongodb    $m = new MongoClient();    echo "Connection to database successfully";    // 選擇一個數(shù)據(jù)庫    $db = $m->mydb;
   echo "Database mydb selected";
?>

執(zhí)行以上程序,輸出結(jié)果如下:

Connection to database successfully
Database mydb selected

創(chuàng)建集合

創(chuàng)建集合的代碼片段如下:

<?php    // 連接到mongodb    $m = new MongoClient();    echo "Connection to database successfully";    // 選擇一個數(shù)據(jù)庫    $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->createCollection("mycol");
   echo "Collection created succsessfully";
?>

執(zhí)行以上程序,輸出結(jié)果如下:

Connection to database successfully
Database mydb selected
Collection created succsessfully

插入文檔

在mongoDB中使用 insert() 方法插入文檔:

插入文檔代碼片段如下:

<?php    // 連接到mongodb    $m = new MongoClient();    echo "Connection to database successfully";    // 選擇一個數(shù)據(jù)庫    $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
   $document = array( 
      "title" => "MongoDB", 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://hgci.cn/mongodb/",
      "by", "w3cschool.cn"
   );
   $collection->insert($document);
   echo "Document inserted successfully";
?>

執(zhí)行以上程序,輸出結(jié)果如下:

Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully

查找文檔

使用find() 方法來讀取集合中的文檔。

讀取使用文檔的代碼片段如下:

<?php    // 連接到mongodb    $m = new MongoClient();    echo "Connection to database successfully";    // 選擇一個數(shù)據(jù)庫    $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";

   $cursor = $collection->find();
   // 迭代顯示文檔標題
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>

執(zhí)行以上程序,輸出結(jié)果如下:

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

更新文檔

使用 update() 方法來更新文檔。

以下實例將更新文檔中的標題為' MongoDB Tutorial', 代碼片段如下:

<pre>
<?php    // 連接到mongodb    $m = new MongoClient();    echo "Connection to database successfully";    // 選擇一個數(shù)據(jù)庫    $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";

   // 更新文檔
   $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB Tutorial")));
   echo "Document updated successfully";
   // 顯示更新后的文檔
   $cursor = $collection->find();
   // 循環(huán)顯示文檔標題
   echo "Updated document";
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>

執(zhí)行以上程序,輸出結(jié)果如下:

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

刪除文檔

使用 remove() 方法來刪除文檔。

以下實例中我們將移除 'title' 為 'MongoDB Tutorial' 的數(shù)據(jù)記錄。, 代碼片段如下:

<?php    // 連接到mongodb    $m = new MongoClient();    echo "Connection to database successfully";    // 選擇一個數(shù)據(jù)庫    $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
   
   // 移除文檔
   $collection->remove(array("title"=>"MongoDB Tutorial"),false);
   echo "Documents deleted successfully";
   
   // 顯示可用文檔數(shù)據(jù)
   $cursor = $collection->find();
   // iterate cursor to display title of documents
   echo "Updated document";
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>

執(zhí)行以上程序,輸出結(jié)果如下:

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

除了以上實例外,在php中你還可以使用findOne(), save(), limit(), skip(), sort()等方法來操作Mongodb數(shù)據(jù)庫。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號