Within Symfony Doctrine i'm using MongoDB as database. I need to have a list of all the collections within a database.
I thought the method ListCollections()
would give me these as the name and documentation suggests.
Except this function doesn't return me anything at all.
From Symfony PHP i'm calling the method like this:
$this->get('doctrine_mongodb.odm.default_connection')->selectDatabase('database');
This gives me an instance of the class Doctrine\MongoDB\Database
This class has the function listCollection()
as stated below:
/**
* Wrapper method for MongoDB::listCollections().
*
* @see http://php.net/manual/en/mongodb.listcollections.php
* @return array
*/
public function listCollections()
{
return $this->mongoDB->listCollections();
}
As the documentation would suggest this function should return an array which it does (yet it's empty even tho there are collections within my database).
So I moved deeper and looked at the documentation of listCollections()
of the mongo pecl plugin which states:
/**
* (PECL mongo >= 0.9.0)<br/>
* Get a list of collections in this database
* @link http://www.php.net/manual/en/mongodb.listcollections.php
* @param bool $includeSystemCollections [optional] <p>Include system collections.</p>
* @return array Returns a list of MongoCollections.
*/
public function listCollections($includeSystemCollections = false) {}
How would one get all the collection names within one database within Symfony 3.0.6?
Some critical details:
If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.
To list all collections in Mongo shell, you can use the function getCollectionNames().
Mongo 1.5.5 has a bug which creates this.
https://github.com/mongodb/mongo-php-driver-legacy/blob/1.5.5/db.c
Try updating the mongo extension to 1.6.14 and try again!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With