MongoDB\Database::listCollectionNames()
On this page
New in version 1.7.
Definition
Parameters
$options
: arrayAn array specifying the desired options.
NameTypeDescriptionauthorizedCollections
boolean
A flag that determines which collections are returned based on the user privileges when access control is enabled. For more information, see the listCollections command documentation.
For servers < 4.0, this option is ignored.
New in version 1.12.
comment
mixed
Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.
This option is available since MongoDB 4.4 and will result in an exception at execution time if specified for an older server version.
New in version 1.13.
filter
array|object
A query expression to filter the list of collections.
You can specify a query expression for collection fields (e.g.
name
,options
).maxTimeMS
integer
The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
session
Client session to associate with the operation.
Return Values
An Iterator, which provides the name of each collection in the database.
Example
The following example lists all of the collections in the test
database:
$database = (new MongoDB\Client)->test; foreach ($database->listCollectionNames() as $collectionName) { var_dump($collectionName); }
The output would then resemble:
string(11) "restaurants" string(5) "users" string(6) "restos"
The following example lists all collections whose name starts with "rest"
in the test
database:
$database = (new MongoDB\Client)->test; $collections = $database->listCollectionNames([ 'filter' => [ 'name' => new MongoDB\BSON\Regex('^rest.*'), ], ]); foreach ($collections as $collectionName) { var_dump($collectionName); }
The output would then resemble:
string(11) "restaurants" string(6) "restos"
Note
When enumerating collection names, a filter expression can only filter based on a collection's name and type. No other fields are available.
See Also
listCollections command reference in the MongoDB manual
Enumerating Collections specification