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

MongoDB\Database::listCollectionNames()

On this page

  • Definition
  • Parameters
  • Return Values
  • Example
  • See Also

New in version 1.7.

MongoDB\Database::listCollectionNames()

Returns names for all collections in this database.

function listCollectionNames(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.

An Iterator, which provides the name of 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->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:

<?php
$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.

  • MongoDB\Database::listCollections()

  • listCollections command reference in the MongoDB manual

  • Enumerating Collections specification

Back

getWriteConcern()