How to get databases names from listDatabases in mongodb?
NickName:Nida Amin Ask DateTime:2018-01-15T20:06:49

How to get databases names from listDatabases in mongodb?

I am getting objects from below query in mongodb in variable

     $dbases = $this->connection->listDatabases(); 

     var_dump($dbases);       

like

        object(MongoDB\Model\DatabaseInfoLegacyIterator)#10 (1) { ["databases":"MongoDB\Model\DatabaseInfoLegacyIterator":private]=> array(5) { [0]=> array(3) { ["name"]=> string(3) "MGL" ["sizeOnDisk"]=> float(3596288) ["empty"]=> bool(false) } [1]=> array(3) { ["name"]=> string(10) "SiteStatus" ["sizeOnDisk"]=> float(32768) ["empty"]=> bool(false) } [2]=> array(3) { ["name"]=> string(5) "admin" ["sizeOnDisk"]=> float(32768) ["empty"]=> bool(false) } [3]=> array(3) { ["name"]=> string(6) "config" ["sizeOnDisk"]=> float(12288) ["empty"]=> bool(false) } [4]=> array(3) { ["name"]=> string(5) "local" ["sizeOnDisk"]=> float(73728) ["empty"]=> bool(false) } } } 

I want to extract database names only like MGL,SiteStatus from this object. Please help me in extracting them through php code. I have tried like

            foreach ($dbases['databases'] as $dbs) 
            {    
                $dbname = $dbs['name'];                              
            }

It is not working. Please help!!!

Copyright Notice:Content Author:「Nida Amin」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48262704/how-to-get-databases-names-from-listdatabases-in-mongodb

More about “How to get databases names from listDatabases in mongodb?” related questions

How to get databases names from listDatabases in mongodb?

I am getting objects from below query in mongodb in variable $dbases = $this->connection->listDatabases(); var_dump($dbases); like object(MongoDB\Model\

Show Detail

Unable to fetch names of databases in MongoDB using PHP with command 'listdatabases'

I have PHP 7.2.30, MongoDB v4.2.8 and installed respective php-mongodb driver using sudo pecl install mongodb command. Screenshot to my phpinfo() is attached which contains details of mongodb. Ult...

Show Detail

How to give diagnostic actions listDatabases to a user in mongodb?

I'm trying to connect from a java application to a mongodb database in openshift and I'm having some trouble with roles and actions allowed. I run the rch port-forward command like so: rhc port-f...

Show Detail

Connection to a query router (mongos) from C# and list databases

I nee to connect to a mongo and run a commands. I'm am connecting using the following piece of code. I want to test weather I am connecting by listing the databases. MongoClient client = new

Show Detail

PHP 7 MongoDB - drop all databases

I'm trying to unit test my mongodb methods, thus I need a tearDown() method to flush my entire mongo database after testing. Here is the function: public function tearDown() { $databases = $t...

Show Detail

Mongodb native connections from nodejs return undefined databases list

I'm just starting to use Mongodb without mongoose (to get away from the schemas), and wanted to create a simple module with various exported functions to use in the rest of my app. I've pasted the ...

Show Detail

Retrieve MongoDB\Client from \MongoDB\Driver\Manager

I use \MongoDB\Driver\Manager to manage my connections, but I do not use MongoDB\Client. I'm trying to list databases given a connection but the method listDatabases is only available via MongoDB\C...

Show Detail

List all MongoDB Databases and their details from Java

I am developing a Java/MongoDB application and require a list of all existing MongoDB Databases. I know I can use this code:- final MongoClient mongoClient = DatabaseManager.getMongoclient(); fi...

Show Detail

Mongodb not authorized on admin to execute listDatabases command

After droping mongodb user ,not able to reconnect mongo again without authentication i had created superuser tomuser my command sequence in ubuntu terminal 1.use admin 2.db.dropUser('tomuser')...

Show Detail

Print the names of collections with mongoDB and node.js

im using mongoclient and im sure that I have connection to the database const client = new MongoClient(uri); const datatbaseslist = client.db().admin().listDatabases(); datatbaseslist.databases.

Show Detail