PHP Velho Oeste 2024

MongoDB::listCollections

(PECL mongo >=0.9.0)

MongoDB::listCollectionsこのデータベース内のすべてのコレクションの MongoCollection オブジェクトの配列を取得する

説明

public MongoDB::listCollections ([ array $options = array() ] ) : array

データベース内のすべてのコレクションのリストを取得し、 MongoCollection オブジェクトの配列で返します。

注意: このメソッドは、MongoDB 2.8 以降と通信する際に、データベースコマンド » listCollections を利用します。以前のバージョンのデータベースの場合は、特別なコレクション system.namespaces を問い合わせます。

パラメータ

options

コレクションの一覧を取得する際のオプションの配列。現在利用可能なオプションは、以下のとおりです。

  • "filter"

    オプションの問い合わせ条件。これを指定すると、結果に含まれるコレクションをその条件でフィルタリングします。

    問い合わせの対象となる関連フィールドには、"name" (コレクション名を表す文字列。データベース名のプレフィックスは含まない) や "options" (コレクションを作成するために用いるオプションを含むオブジェクト) があります。

    注意: MongoDB 2.6 以前のバージョンでは、"name" の条件指定には文字列しか使えませんでした (一致する文字列だけに絞り込むなど)。これは、ドライバが system.namespaces コレクションに問い合わせるときに、その値をデータベース名の先頭に付加する必要があったからです。最新版の MongoDB にはこの制約がなくなりました。listCollections コマンドを使うようになったからです。

  • "includeSystemCollections"

    Boolean で、デフォルトは FALSE です。system コレクションを結果に含めるかどうかを指定します。

以下のオプションは、MongoDB 2.8 以降で利用可能です。

  • "maxTimeMS"

    サーバー上で操作を行う累積時間の制限 (アイドル時間を含まない) を、ミリ秒単位で指定します。この時間内にサーバー側の操作が完了しなければ、MongoExecutionTimeoutException をスローします。

返り値

MongoCollection オブジェクトの配列を返します。

エラー / 例外

MongoDB 2.6 およびそれ以前のバージョンでは、 "filter" オプションの "name" に文字列以外の値を指定した場合に MongoException をスローします。

変更履歴

バージョン 説明
1.6.0 最初のパラメータがオプションの配列に変わりました。以前のバージョンでは最初のパラメータは boolean で、 "includeSystemCollections" オプションの値を指定するものでした。
1.3.0 includeSystemCollections が追加されました。

例1 MongoDB::listCollections() の例

次の例は、データベース内の各コレクションの件数を数えます。

<?php

$m 
= new MongoClient();
$db $m->selectDB("demo");
$collections $db->listCollections();

foreach (
$collections as $collection) {
    echo 
"amount of documents in $collection: ";
    echo 
$collection->count(), "\n";
}

?>

上の例の出力は、 たとえば以下のようになります。

...
amount of documents in demo.pubs: 4
amount of documents in demo.elephpants: 3
amount of documents in demo.cities: 22840
...

参考

add a note add a note

User Contributed Notes 1 note

up
0
George Gombay
9 years ago
For an alternative to the shell command 'show dbs', refer to my note for the listDBs() method of Mongoclient(), which reproduces some simple PHP code that will yield the names of all database present.
To Top