sashavalentina Posted February 7, 2023 Share Posted February 7, 2023 Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\Manager::listDatabases() this error keep showing when i wanted to use the administration command for mongodb using php. I do not know whats the problem here and someone with kind soul please help me. The following codes is what i have tried which cause that error. <?php $client = new MongoDB\Driver\Manager( 'mongodb+srv://'.$_ENV['MDB_USER'].':'.$_ENV['MDB_PASS'].'@'.$_ENV['ATLAS_CLUSTER_SRV'].'/test' ); try{ $dbs = $client->listDatabases(); echo '<pre>'; print_r($dbs); echo '</pre>'; // Or Nothing if you just wanna check for errors } catch(Exception $e){ echo "Unable to connect to Database at the moment ! "; exit(); } $colecciones = $client->listCollections(); foreach ($colecciones as $col) { echo $col->getName(); } ?> what i am trying to do here is to make sure that my database connection is successful and also list out the collection name of my mongodb database. Quote Link to comment https://forums.phpfreaks.com/topic/315889-why-i-cant-use-the-administration-command-in-php-when-i-am-working-in-mongodb/ Share on other sites More sharing options...
requinix Posted February 7, 2023 Share Posted February 7, 2023 Why do you think that MongoDB\Driver\Manager has a method named "listDatabases"? I don't see it. Quote Link to comment https://forums.phpfreaks.com/topic/315889-why-i-cant-use-the-administration-command-in-php-when-i-am-working-in-mongodb/#findComment-1605449 Share on other sites More sharing options...
gizmola Posted February 10, 2023 Share Posted February 10, 2023 You are using the low level driver. You want to use the mongo client library which does have that method. Quote Link to comment https://forums.phpfreaks.com/topic/315889-why-i-cant-use-the-administration-command-in-php-when-i-am-working-in-mongodb/#findComment-1605534 Share on other sites More sharing options...
aarti789 Posted August 25, 2023 Share Posted August 25, 2023 Well, there are some logical error with your code, you can try below code to do this. <?php require 'vendor/autoload.php'; // Make sure to include the MongoDB PHP library autoload file $client = new MongoDB\Client('mongodb+srv://'.$_ENV['MDB_USER'].':'.$_ENV['MDB_PASS'].'@'.$_ENV['ATLAS_CLUSTER_SRV'].'/test'); try { $databases = $client->listDatabases(); foreach ($databases as $databaseInfo) { echo "Database: " . $databaseInfo['name'] . "\n"; } } catch (Exception $e) { echo "Unable to connect to the database at the moment!"; exit(); } $database = $client->selectDatabase('your_database_name'); // Replace with your actual database name $collectionNames = $database->listCollectionNames(); foreach ($collectionNames as $collectionName) { echo "Collection: " . $collectionName . "\n"; } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/315889-why-i-cant-use-the-administration-command-in-php-when-i-am-working-in-mongodb/#findComment-1611412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.