
- 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 - 建立集合
執行任何操作的第一步是建立一個管理器例項。
// 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.
廣告