Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB::listCollections not working

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:

  • Mongo version: 1.5.5
  • Symfony version: 3.0.6
  • MongoDB shell version: 3.2.4
like image 847
Baklap4 Avatar asked May 25 '16 13:05

Baklap4


People also ask

How do I display a table in MongoDB?

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.

How do I list collections in MongoDB shell?

To list all collections in Mongo shell, you can use the function getCollectionNames().


1 Answers

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!

like image 73
Jan Doornbos Avatar answered Oct 24 '22 21:10

Jan Doornbos