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

   This method has the following parameters:

   .. include:: /includes/apiargs/MongoDBClient-method-listDatabases-param.rst

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

   .. include:: /includes/apiargs/MongoDBClient-method-listDatabases-option.rst

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

   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
