©
本文檔使用 php中文網手册 發布
(PECL mongo >=0.9.0)
MongoDB::listCollections — Gets an array of MongoCollection objects for all collections in this database
$options
= array()
] )Gets a list of all collections in the database and returns them as an array of MongoCollection objects.
Note: This method will use the » listCollections database command when communicating with MongoDB 2.8+. For previous database versions, the method will query the special system.namespaces collection.
options
An array of options for listing the collections. Currently available options include:
"filter"
Optional query criteria. If provided, this criteria will be used to filter the collections included in the result.
Relevant fields that may be queried include "name" (collection name as a string, without the database name prefix) and "options" (object containing options used to create the collection)..
Note: MongoDB 2.6 and earlier versions require the "name" criteria, if specified, to be a string value (i.e. equality match). This is because the driver must prefix the value with the database name in order to query the system.namespaces collection. Later versions of MongoDB do not have this limitation, as the driver will use the listCollections command.
"includeSystemCollections"
Boolean, defaults to FALSE
. Determines whether system collections should be included in the result.
The following option may be used with MongoDB 2.8+:
"maxTimeMS"
Specifies a cumulative time limit in milliseconds for processing the operation (does not include idle time). If the operation is not completed within the timeout period, a MongoExecutionTimeoutException will be thrown.
Returns an array of MongoCollection objects.
For MongoDB 2.6 and earlier, MongoException will be thrown if a non-string value was specified for the "filter" option's "name" criteria.
版本 | 说明 |
---|---|
1.6.0 | Changed first parameter to be an array of options. Pre-1.6.0, the first parameter was a boolean indicating the "includeSystemCollections" option. |
1.3.0 |
Added the includeSystemCollections parameter.
|
Example #1 MongoDB::listCollections() example
The following example demonstrates running count on each collection in a database.
<?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 ...
[#1] George Gombay [2014-06-02 20:53:48]
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.