MongoDBDatabase-listCollections.txt 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
====================================
MongoDB\\Database::listCollections()
====================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Database::listCollections()
17 18 19 20 21

   Returns information for all collections in this database.

   .. code-block:: php

22 23
      function listCollections(array $options = []): MongoDB\Model\CollectionInfoIterator

24
   This method has the following parameters:
25

26
   .. include:: /includes/apiargs/MongoDBDatabase-method-listCollections-param.rst
27 28 29

   The ``$options`` parameter supports the following options:

30 31
   .. include:: /includes/apiargs/MongoDBDatabase-method-listCollections-option.rst

32
   :returns:
33

34 35 36
      A traversable :phpclass:`MongoDB\\Model\CollectionInfoIterator`, which
      contains an :phpclass:`MongoDB\\Model\CollectionInfo` object for each
      collection in the database.
37 38 39 40

Example
-------

41
The following example lists all of the collections in the ``example`` database:
42 43 44 45

.. code-block:: php

   <?php
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   $database = (new MongoDB\Client)->example;

   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) {
     }
   }
62
   object(MongoDB\Model\CollectionInfo)#3 (2) {
63 64 65 66 67 68 69 70 71 72 73 74 75
     ["name"]=>
     string(5) "users"
     ["options"]=>
     array(0) {
     }
   }
   object(MongoDB\Model\CollectionInfo)#3 (2) {
     ["name"]=>
     string(6) "restos"
     ["options"]=>
     array(0) {
     }
   }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

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

.. code-block:: php

   <?php

   $database = (new MongoDB\Client)->example;

   $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) {
     }
   }

113 114
.. seealso::

115 116 117 118 119
   - :manual:`listCollections </reference/command/listCollections` command
     reference in the MongoDB manual
   - `Enumerating Collections
     <https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst>`_
     specification