Php Mongodb 简明教程
PHP & MongoDB - Show Databases
执行任何操作的第一步是创建一个管理器实例。
// Connect to MongoDB using Manager Instance
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
第二步是准备并执行一个数据库上的命令,以显示可用的数据库列表。
// Create a Command Instance
$databaseList = new MongoDB\Driver\Command(["listDatabases" => 1]);
// Execute the command on the database
$cursor = $manager->executeCommand("admin", $databaseList);
Example
尝试以下示例,以列出 MongoDB 服务器中默认情况下可用的数据库 −
将以下示例复制粘贴到 mongodb_example.php −
<?php
try {
// connect to mongodb
$manager = new MongoDB\Driver\Manager("mongodb://localhost: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";
}
?>