================================
MongoDB\\Client::listDatabases()
================================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\Client::listDatabases()

   Returns information for all databases on the server.

   .. code-block:: php

      function listDatabases(array $options = []): MongoDB\Model\DatabaseInfoIterator

Parameters
----------

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - authorizedDatabases
       - boolean
       - A flag that determines which databases are returned based on the user
         privileges when access control is enabled. For more information, see the
         `listDatabases command documentation <https://mongodb.com/docs/manual/reference/command/listDatabases/>`_.

         For servers < 4.0.5, this option is ignored.

         .. versionadded:: 1.7

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

         .. include:: /includes/extracts/option-requires-4.4.rst

         .. versionadded:: 1.13

     * - filter
       - array|object
       - A query expression to filter the list of databases.

         You can specify a query expression for database fields (e.g. ``name``,
         ``sizeOnDisk``, ``empty``).

         .. versionadded:: 1.3

     * - maxTimeMS
       - integer
       - .. include:: /includes/extracts/common-option-maxTimeMS.rst

     * - session
       - :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
       - .. include:: /includes/extracts/common-option-session.rst

         .. versionadded:: 1.3



Return Values
-------------

A traversable :phpclass:`MongoDB\Model\DatabaseInfoIterator`, which contains
a :phpclass:`MongoDB\Model\DatabaseInfo` object for each database on the
server.

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Example
-------

The following example lists all databases on the server:

.. code-block:: php

   <?php

   $client = new MongoDB\Client;

   foreach ($client->listDatabases() as $databaseInfo) {
       var_dump($databaseInfo);
   }

The output would then resemble:

.. code-block:: none

   object(MongoDB\Model\DatabaseInfo)#4 (3) {
     ["name"]=>
     string(5) "local"
     ["sizeOnDisk"]=>
     float(65536)
     ["empty"]=>
     bool(false)
   }
   object(MongoDB\Model\DatabaseInfo)#7 (3) {
     ["name"]=>
     string(4) "test"
     ["sizeOnDisk"]=>
     float(32768)
     ["empty"]=>
     bool(false)
   }

See Also
--------

- :phpmethod:`MongoDB\Client::listDatabaseNames()`
- :manual:`listDatabases </reference/command/listDatabases>` command reference
  in the MongoDB manual
- `Enumerating Databases
  <https://github.com/mongodb/specifications/blob/master/source/enumerate-databases.rst>`_
  specification
