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.
廣告