|
| 1 | +======================================== |
| 2 | +MongoDB\\Database::listCollectionNames() |
| 3 | +======================================== |
| 4 | + |
| 5 | +.. versionadded:: 1.7 |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Definition |
| 16 | +---------- |
| 17 | + |
| 18 | +.. phpmethod:: MongoDB\\Database::listCollectionNames() |
| 19 | + |
| 20 | + Returns names for all collections in this database. |
| 21 | + |
| 22 | + .. code-block:: php |
| 23 | + |
| 24 | + function listCollectionNames(array $options = []): Iterator |
| 25 | + |
| 26 | + This method has the following parameters: |
| 27 | + |
| 28 | + .. include:: /includes/apiargs/MongoDBDatabase-method-listCollections-param.rst |
| 29 | + |
| 30 | + The ``$options`` parameter supports the following options: |
| 31 | + |
| 32 | + .. include:: /includes/apiargs/MongoDBDatabase-method-listCollections-option.rst |
| 33 | + |
| 34 | +Return Values |
| 35 | +------------- |
| 36 | + |
| 37 | +An :php:`Iterator <class.iterator.php>`, which provides the name of each |
| 38 | +collection in the database. |
| 39 | + |
| 40 | +Example |
| 41 | +------- |
| 42 | + |
| 43 | +The following example lists all of the collections in the ``test`` database: |
| 44 | + |
| 45 | +.. code-block:: php |
| 46 | + |
| 47 | + <?php |
| 48 | + |
| 49 | + $database = (new MongoDB\Client)->test; |
| 50 | + |
| 51 | + foreach ($database->listCollectionNames() as $collectionName) { |
| 52 | + var_dump($collectionName); |
| 53 | + } |
| 54 | + |
| 55 | +The output would then resemble:: |
| 56 | + |
| 57 | + string(11) "restaurants" |
| 58 | + string(5) "users" |
| 59 | + string(6) "restos" |
| 60 | + |
| 61 | +The following example lists all collections whose name starts with ``"rest"`` |
| 62 | +in the ``test`` database: |
| 63 | + |
| 64 | +.. code-block:: php |
| 65 | + |
| 66 | + <?php |
| 67 | + |
| 68 | + $database = (new MongoDB\Client)->test; |
| 69 | + |
| 70 | + $collections = $database->listCollectionNames([ |
| 71 | + 'filter' => [ |
| 72 | + 'name' => new MongoDB\BSON\Regex('^rest.*'), |
| 73 | + ], |
| 74 | + ]); |
| 75 | + |
| 76 | + foreach ($collections as $collectionName) { |
| 77 | + var_dump($collectionName); |
| 78 | + } |
| 79 | + |
| 80 | +The output would then resemble:: |
| 81 | + |
| 82 | + string(11) "restaurants" |
| 83 | + string(6) "restos" |
| 84 | + |
| 85 | +.. note:: |
| 86 | + |
| 87 | + When enumerating collection names, a filter expression can only filter based |
| 88 | + on a collection's name and type. No other fields are available. |
| 89 | + |
| 90 | +See Also |
| 91 | +-------- |
| 92 | + |
| 93 | +- :phpmethod:`MongoDB\\Database::listCollections()` |
| 94 | +- :manual:`listCollections </reference/command/listCollections` command |
| 95 | + reference in the MongoDB manual |
| 96 | +- `Enumerating Collections |
| 97 | + <https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst>`_ |
| 98 | + specification |
0 commit comments