1

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

2
  • Try foreach ($dbases->databases as $dbs) Commented Jan 15, 2018 at 12:10
  • Cannot access private property MongoDB\Model\DatabaseInfoLegacyIterator::$databases i Commented Jan 15, 2018 at 12:17

1 Answer 1

2

You could try to iterate over the Iterator, like this:

foreach($this->connection->listDatabases() as $database)
{
    echo $database->getName();
}

Each $database is an object of class MongoDB\Model\DatabaseInfo which has a method getName().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.