php教程

MongoClient::listDBs

(PECL mongo >=1.3.0)

MongoClient::listDBs列出所有有效数据库

This extension that defines this method is deprecated. Instead, the MongoDB extension should be used. There is no equivalent for this method in the new extension, but there is an alternative in the PHP library:

说明

public MongoClient::listDBs ( void ) : array

参数

此函数没有参数。

返回值

返回的关联数组包括了三个字段。 第一个字段是 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
)

User Contributed Notes

George Gombay 02-Jun-2014 10:41
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

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788