Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\Database::listCollections()

On this page

  • Definition
  • Parameters
  • Return Values
  • Example
  • See Also
MongoDB\Database::listCollections()

Returns information for all collections in this database.

function listCollections(array $options = []): Iterator
$options : array

An array specifying the desired options.

Name
Type
Description

authorizedCollections

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.

New in version 1.3.

An Iterator instance, which provides a MongoDB\Model\CollectionInfo object for each collection in the database.

The following example lists all of the collections in the test database:

<?php
$database = (new MongoDB\Client)->test;
foreach ($database->listCollections() as $collectionInfo) {
var_dump($collectionInfo);
}

The output would then resemble:

object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(11) "restaurants"
["options"]=>
array(0) {
}
}
object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(5) "users"
["options"]=>
array(0) {
}
}
object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(6) "restos"
["options"]=>
array(0) {
}
}

The following example lists all collections whose name starts with "rest" in the test database:

<?php
$database = (new MongoDB\Client)->test;
$collections = $database->listCollections([
'filter' => [
'name' => new MongoDB\BSON\Regex('^rest.*'),
],
]);
foreach ($collections as $collectionInfo) {
var_dump($collectionInfo);
}

The output would then resemble:

object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(11) "restaurants"
["options"]=>
array(0) {
}
}
object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(6) "restos"
["options"]=>
array(0) {
}
}

Back

listCollectionNames()