©
本文档使用
php.cn手册 发布
(PECL mongo >=1.3.0)
MongoClient::listDBs — 列出所有有效数据库
此函数没有参数。
返回的关联数组包括了三个字段。 第一个字段是 databases,里面包含了一个数组。每个元素对应一个数据库,给出数据库名称、尺寸以及是否为空。 另外两个字段是 totalSize(单位为字节 bytes)和 ok,如果方法成功运行,它会是 1。
Example #1 MongoClient::listDBs() 例子
例子演示了如何列出数据库,并返回数据的结构。
<?php
$mongo = new MongoClient ();
$dbs = $mongo -> listDBs ();
print_r ( $dbs );
?>
以上例程的输出类似于:
Array ( [databases] => Array ( [0] => Array ( [name] => doctrine [sizeOnDisk] => 218103808 [empty] => ) ) [totalSize] => 218103808 [ok] => 1 )
[#1] George Gombay [2014-06-02 10:41:13]
A "no frills" listing of all the databases present can be obtained by means of the simple following steps:
<?php
$conn = new MongoClient("mongodb://localhost");
$dbases = $conn->listDBs();
$num = 0;
foreach ($dbases['databases'] as $dbs) {
$num++;
$dbname = $dbs['name'];
echo "<br> $num. $dbname";
}
?>
On the assumption that you have three databases present, the foregoing will produce an output that will look similar to this:
1. local
2. members
3. test