0

I need help in referencing the name of each individual database attached to a MongoDB instance. I just want to connect to a database with PHP and list the names of all the databases.

In the code below I can connect to MongoDB instance and find all the databases. I need to extract the name of the databases to place them in a list box and then to do the same for all the collections within each database. First I need to know is how to reference the name of the databases I see when the following code is used.
I have tried various version of [$showField = $databaseInfo->name;, $showField = $databaseInfo[name];] but I don’t know the correct grammar and would appreciate help.

// This path should point to Composer's autoloader
            require_once __DIR__ . "/vendor/autoload.php";

            // Find if we can connect to a Mongo instance
            // Find all the Databasses on this Mongo instance
            $client = new MongoDB\Client;
            if ($client) {
                echo (" Made a connection");
            }
            // Count databases
            $i = 0;
            /* listDatabases() returns an iterator of MongoDB\Model\DatabaseInfo objects */
            foreach ($client->listDatabases() as $databaseInfo) {
                var_dump($databaseInfo);
                $i++;
                /*  
                $showField = $databaseInfo->name;
                echo $showField;
                */
            }

            ?>

Here is my system:
Windows 10
Thread Safety enabled in PHP7 PHP 7.0.2 (cli) (built: Jan 6 2016 13:00:05) ( ZTS )
MongoDB shell version: 3.2.1
MongoDB PHP Library 1.0

2
Contributors
2
Replies
12
Views
1 Year
Discussion Span
Last Post by na3eem
0

I found a method called getName() which will solve this problem.

$dbNames[$i] = $databaseInfo->getName();

That will work.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.