- 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 - 删除集合
执行任何操作的第一步都是创建一个 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.
广告