
- PHP & MongoDB 教程
- PHP & MongoDB - 首頁
- PHP & MongoDB - 概述
- PHP & MongoDB - 環境設定
- PHP & MongoDB 示例
- PHP & MongoDB - 連線資料庫
- PHP & MongoDB - 顯示資料庫
- PHP & MongoDB - 刪除資料庫
- PHP & MongoDB - 建立集合
- PHP & MongoDB - 刪除集合
- PHP & MongoDB - 顯示集合
- PHP & MongoDB - 插入文件
- PHP & MongoDB - 選擇文件
- PHP & MongoDB - 更新文件
- PHP & MongoDB - 刪除文件
- PHP & MongoDB - 嵌入式文件
- PHP & MongoDB - 錯誤處理
- PHP & MongoDB - 限制記錄
- PHP & MongoDB - 排序記錄
- PHP & MongoDB 有用資源
- PHP & MongoDB - 快速指南
- PHP & MongoDB - 有用資源
- PHP & MongoDB - 討論
PHP & MongoDB - 快速指南
PHP & MongoDB - 概述
PHP 開發團隊提供了 MongoDB PHP 驅動程式,並提供了各種相關資源。
使用 PHP 連線 MongoDB 的第一步是在 php ext 目錄中擁有 mongodb PHP 驅動程式 dll,並在 php.ini 中啟用它,然後使用 mongodb API 連線到資料庫。
連線到 MongoDB 資料庫
假設 MongoDB 安裝在本地並使用預設埠,則以下語法連線到 MongoDB 資料庫。
$manager = new MongoDB\Driver\Manager("mongodb://:27017");
MongoDB 驅動程式管理器是執行所有 mongodb 資料庫操作的中心類。要連線到 mongodb 資料庫,我們首先準備一個命令,然後執行它來連線到資料庫。
$statistics = new MongoDB\Driver\Command(["dbstats" => 1]); $cursor = $manager->executeCommand("mydb", $statistics);
命令執行後,如果 mydb 資料庫不存在,則會建立它,否則會連線到它。
PHP & MongoDB - 環境設定
安裝 MongoDB 資料庫
按照使用 MongoDB - 環境設定 的 MongoDB 安裝步驟進行操作。
安裝 PHP
按照使用 PHP 7 - 環境設定 的 PHP 安裝步驟進行操作。
PHP MongoDB 驅動程式
要將 MongoDB 與 PHP 一起使用,您需要使用 MongoDB PHP 驅動程式。從網址 下載 PHP 驅動程式 下載驅動程式。確保下載其最新版本。現在解壓縮存檔並將 php_mongo.dll 放入您的 PHP 擴充套件目錄(預設情況下為“ext”),並將以下行新增到您的 php.ini 檔案中:
extension = php_mongo.dll
在 Windows 中,請確保 libsasl.dll 位於 windows 的 PATH 中。此 dll 可在 PHP 安裝目錄中找到。
PHP & MongoDB - 連線資料庫
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並在資料庫上執行一個命令,如果資料庫不存在,則 MongoDB 會自動建立它。
// Create a Command Instance $statistics = new MongoDB\Driver\Command(["dbstats" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $statistics);
示例
嘗試以下示例以連線到 MongoDB 伺服器:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); echo "Connection to database successfully"; $statistics = new MongoDB\Driver\Command(["dbstats" => 1]); $cursor = $manager->executeCommand("myDb", $statistics); $statistics = current($cursor->toArray()); echo "<pre>"; print_r($statistics); echo "</pre>"; } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Connection to database successfully stdClass Object ( [db] => myDb [collections] => 0 [views] => 0 [objects] => 0 [avgObjSize] => 0 [dataSize] => 0 [storageSize] => 0 [totalSize] => 0 [indexes] => 0 [indexSize] => 0 [scaleFactor] => 1 [fileSize] => 0 [fsUsedSize] => 0 [fsTotalSize] => 0 [ok] => 1 )
PHP & MongoDB - 顯示資料庫
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並在資料庫上執行一個命令來顯示可用的資料庫列表。
// Create a Command Instance $databaseList = new MongoDB\Driver\Command(["listDatabases" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("admin", $databaseList);
示例
嘗試以下示例以列出 MongoDB 伺服器中預設可用的資料庫:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); // Create a Command Instance $databaseList = new MongoDB\Driver\Command(["listDatabases" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("admin", $databaseList); $databases = current($cursor->toArray()); foreach ($databases->databases as $database) { echo $database->name . "<br/>"; } } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
admin config local
PHP & MongoDB - 刪除資料庫
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個命令來刪除資料庫。
// Create a Command Instance $dropDatabase = new MongoDB\Driver\Command(["dropDatabase" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $dropDatabase);
示例
嘗試以下示例以刪除 MongoDB 伺服器中的資料庫:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); // Create a Command Instance $dropDatabase = new MongoDB\Driver\Command(["dropDatabase" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $dropDatabase); echo "Database dropped." } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Database dropped.
PHP & MongoDB - 建立集合
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個命令來建立集合。
// Create a Command Instance $createCollection = new MongoDB\Driver\Command(["create" => "sampleCollection"]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $createCollection);
示例
嘗試以下示例以在 MongoDB 伺服器中建立集合:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); // Create a Command Instance $createCollection = new MongoDB\Driver\Command(["create" => "sampleCollection"]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $createCollection); echo "Collection created."; } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Collection created.
PHP & MongoDB - 刪除集合
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個命令來刪除集合。
// Create a Command Instance $createCollection = new MongoDB\Driver\Command(["drop" => "sampleCollection"]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $createCollection);
示例
嘗試以下示例以刪除 MongoDB 伺服器中的集合:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); // Create a Command Instance $dropCollection = new MongoDB\Driver\Command(["drop" => "sampleCollection"]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $dropCollection); echo "Collection dropped." } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Collection dropped.
PHP & MongoDB - 顯示集合
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並在資料庫上執行一個命令來顯示可用的集合列表。
// Create a Command Instance $collectionList = new MongoDB\Driver\Command(["listCollections" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $collectionList);
示例
嘗試以下示例以列出 MongoDB 伺服器中資料庫的集合:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); // Create a Command Instance $collectionList = new MongoDB\Driver\Command(["listCollections" => 1]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $collectionList); $collections = $cursor->toArray(); foreach ($collections as $collection) { echo $collection->name . "<br/>"; } } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
sampleCollection
PHP & MongoDB - 插入文件
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 bulkWrite 物件以在集合中插入記錄。
// Create a BulkWrite Object $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->insert(['First_Name' => "Mahesh", 'Last_Name' => 'Parashar', 'Date_Of_Birth' => '1990-08-21', 'e_mail' => 'mahesh_parashar.123@gmail.com', 'phone' => '9034343345']); // Execute the commands. $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk);
示例
嘗試以下示例以在 MongoDB 伺服器中的集合中插入文件:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->insert(['First_Name' => "Mahesh", 'Last_Name' => 'Parashar', 'Date_Of_Birth' => '1990-08-21', 'e_mail' => 'mahesh_parashar.123@gmail.com', 'phone' => '9034343345']); $bulk->insert(['First_Name' => "Radhika", 'Last_Name' => 'Sharma', 'Date_Of_Birth' => '1995-09-26', 'e_mail' => 'radhika_sharma.123@gmail.com', 'phone' => '9000012345']); $bulk->insert(['First_Name' => "Rachel", 'Last_Name' => 'Christopher', 'Date_Of_Birth' => '1990-02-16', 'e_mail' => 'rachel_christopher.123@gmail.com', 'phone' => '9000054321']); $bulk->insert(['First_Name' => "Fathima", 'Last_Name' => 'Sheik', 'Date_Of_Birth' => '1990-02-16', 'e_mail' => 'fathima_sheik.123@gmail.com', 'phone' => '9000012345']); // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk); printf("Inserted %d document(s).\n", $result->getInsertedCount()); } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Inserted 4 document(s).
PHP & MongoDB - 選擇文件
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 Query 物件以在集合中選擇記錄。
// Create a Query Object $query = new MongoDB\Driver\Query(['First_Name' => "Radhika"]); // Execute the query $rows = $manager->executeQuery("testdb.sampleCollection", $query);
示例
嘗試以下示例以在 MongoDB 伺服器中選擇文件:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $filter = []; // Create a Query Object $query = new MongoDB\Driver\Query($filter); // Execute the query $rows = $manager->executeQuery("myDb.sampleCollection", $query); foreach ($rows as $row) { printf("First Name: %s, Last Name: %s.<br/>", $row->First_Name, $row->Last_Name); } } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
First Name: Radhika, Last Name: Sharma. First Name: Rachel, Last Name: Christopher. First Name: Fathima, Last Name: Sheik.
PHP & MongoDB - 更新文件
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 bulkWrite 物件以更新集合中的記錄。
// Create a BulkWrite Object $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->update(['First_Name' => "Mahesh"],['$set' => ['e_mail' => 'maheshparashar@gmail.com']]); // Execute the commands. $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk);
示例
嘗試以下示例以更新 MongoDB 伺服器中的文件:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->update(['First_Name' => "Mahesh"],['$set' => ['e_mail' => 'maheshparashar@gmail.com']]); // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk); printf("Updated %d document(s).\n", $result->getModifiedCount()); } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Updated 1 document(s).
PHP & MongoDB - 刪除文件
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 bulkWrite 物件以刪除集合中的記錄。
// Create a BulkWrite Object $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->delete(['First_Name' => "Mahesh"]); // Execute the commands. $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk);
示例
嘗試以下示例以刪除 MongoDB 伺服器中集合的文件:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->delete(['First_Name' => "Mahesh"]); // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk); echo "Document deleted." } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Document deleted.
PHP & MongoDB - 插入嵌入式文件
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 bulkWrite 物件以在集合中插入包含嵌入式文件的記錄。
// Create a BulkWrite Object $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->insert(['title' => "MongoDB Overview", 'description' => 'MongoDB is no SQL database', 'by' => 'tutorials point', 'url' => 'https://tutorialspoint.tw', 'comments' => [ ['user' => "user1", 'message' => "My First Comment", 'dateCreated' => "20/2/2020", 'like' => 0], ['user' => "user2", 'message' => "My Second Comment", 'dateCreated' => "20/2/2020", 'like' => 0], ]]); // Execute the commands. $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk);
示例
嘗試以下示例以在 MongoDB 伺服器中的集合中插入包含嵌入式文件的文件:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]); $bulk->insert(['title' => "MongoDB Overview", 'description' => 'MongoDB is no SQL database', 'by' => 'tutorials point', 'url' => 'https://tutorialspoint.tw', 'comments' => [ ['user' => "user1", 'message' => "My First Comment", 'dateCreated' => "20/2/2020", 'like' => 0], ['user' => "user2", 'message' => "My Second Comment", 'dateCreated' => "20/2/2020", 'like' => 0], ]]); $result = $manager->executeBulkWrite('myDb.sampleCollection', $bulk); printf("Inserted %d document(s).\n", $result->getInsertedCount()); } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Inserted 1 document(s).
PHP & MongoDB - 錯誤處理
如果任何資料庫操作發生異常,MongoDB 驅動程式操作會丟擲異常,並且可以使用以下語法輕鬆捕獲。
try { } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "<br/>"; echo "File:", $e->getFile(), "<br/>"; echo "Line:", $e->getLine(), "<br/>"; }
示例
嘗試以下示例以檢查 MongoDB 伺服器中的資料庫操作錯誤:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); // Create a Command Instance $createCollection = new MongoDB\Driver\Command(["create" => "sampleCollection"]); // Execute the command on the database $cursor = $manager->executeCommand("myDb", $createCollection); echo "Collection created."; } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "<br/>"; echo "File:", $e->getFile(), "<br/>"; echo "Line:", $e->getLine(), "<br/>"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
Exception:Collection already exists. NS: myDb.sampleCollection File: \htdocs\php_mongodb\mongodb_example.php Line:10
PHP & MongoDB - 限制記錄
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 Query 物件以在集合中選擇記錄,並傳遞過濾器和選項以限制搜尋結果。
$filter = []; $options = ['limit' => 1]; // Create a Query Object $query = new MongoDB\Driver\Query($filter, $options); // Execute the query $rows = $manager->executeQuery("testdb.sampleCollection", $query);
示例
嘗試以下示例以限制 MongoDB 伺服器中的搜尋結果:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $filter = []; $options = ['limit' => 1]; // Create a Query Object $query = new MongoDB\Driver\Query($filter, $options); // Execute the query $rows = $manager->executeQuery("myDb.sampleCollection", $query); foreach ($rows as $row) { printf("First Name: %s, Last Name: %s.<br/>", $row->First_Name, $row->Last_Name); } } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
First Name: Radhika, Last Name: Sharma.
PHP & MongoDB - 排序記錄
執行任何操作的第一步是建立一個 Manager 例項。
// Connect to MongoDB using Manager Instance $manager = new MongoDB\Driver\Manager("mongodb://:27017");
第二步是準備並執行一個 Query 物件以在集合中選擇記錄,並傳遞過濾器和選項以對記錄進行排序。
$filter = []; // Sort in Descending Order, For ascending order pass 1 $options = ['sort' => ['First_Name' => -1]]; // Create a Query Object $query = new MongoDB\Driver\Query($filter, $options); // Execute the query $rows = $manager->executeQuery("testdb.sampleCollection", $query);
示例
嘗試以下示例以限制 MongoDB 伺服器中的搜尋結果:
複製並貼上以下示例作為 mongodb_example.php:
<?php try { // connect to mongodb $manager = new MongoDB\Driver\Manager("mongodb://:27017"); $filter = []; $options = ['limit' => 3, 'sort' => ['First_Name' => -1]]; // Create a Query Object $query = new MongoDB\Driver\Query($filter, $options); // Execute the query $rows = $manager->executeQuery("myDb.sampleCollection", $query); foreach ($rows as $row) { printf("First Name: %s, Last Name: %s.<br/>", $row->First_Name, $row->Last_Name); } } catch (MongoDB\Driver\Exception\Exception $e) { echo "Exception:", $e->getMessage(), "\n"; } ?>
輸出
訪問部署在 apache web 伺服器上的 mongodb_example.php 並驗證輸出。
First Name: Radhika, Last Name: Sharma. First Name: Rachel, Last Name: Christopher. First Name: Fathima, Last Name: Sheik.