====================================
MongoDB\\Client::listDatabaseNames()
====================================

.. versionadded:: 1.7

.. default-domain:: mongodb

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

Definition
----------

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

   Returns names for all databases on the server.

   .. code-block:: php

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

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
-------------

An :php:`Iterator <class.iterator.php>`, which provides the name of 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->listDatabaseNames() as $databaseName) {
       var_dump($databaseName);
   }

The output would then resemble:

.. code-block:: none

   string(5) "local"
   string(4) "test"

See Also
--------

- :phpmethod:`MongoDB\Client::listDatabases()`
- :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
