How do I list collections with the new PHP driver?

1,234 views
Skip to first unread message

Johannes Reichardt

unread,
Mar 29, 2017, 4:18:38 PM3/29/17
to mongodb-user
Hi there,

I am fiddeling since hours with no result to retrieve a list of collections. Probably I am overlooking something here.

$manager = new \MongoDB\Driver\Manager($host);
$cmd = new \MongoDB\Driver\Command(['listCollections' => 1]);
$res = $manager->executeCommand($db_name, $cmd);

This gives an error that listCollections is no command but it is listed here. The listCommand (which works) on the other hand doesnt show it.
https://docs.mongodb.com/manual/reference/command/

Should I have to do it differently? The same code with listDatabases works perfectly.

I am using the latest driver for PHP 7.1 on windows that connects to a 2.6 DB on debian.

Any hints on this?

By the way I am not very happy with the general usage of Objects in the driver - they are hard to use and often very confusing.
Associative arrays as defaults are way easier to handle in my opinion. Besides that: MongoDB rocks :)

- Johannes




Jeremy Mikola

unread,
Mar 29, 2017, 4:54:34 PM3/29/17
to mongod...@googlegroups.com
On Wed, Mar 29, 2017 at 11:15 AM, 'Johannes Reichardt' via mongodb-user <mongod...@googlegroups.com> wrote:
Hi there,

I am fiddeling since hours with no result to retrieve a list of collections. Probably I am overlooking something here.

$manager = new \MongoDB\Driver\Manager($host);
$cmd = new \MongoDB\Driver\Command(['listCollections' => 1]);
$res = $manager->executeCommand($db_name, $cmd);

This gives an error that listCollections is no command but it is listed here. The listCommand (which works) on the other hand doesnt show it.
https://docs.mongodb.com/manual/reference/command/

Should I have to do it differently? The same code with listDatabases works perfectly.

I am using the latest driver for PHP 7.1 on windows that connects to a 2.6 DB on debian.

Any hints on this?

The listCollections command was only introduced in MongoDB 3.0. Earlier versions of the database require you to run a query on the system.namespaces collection. In addition to the extension, we suggest that folks also use the PHP library, which implements many of the high-level APIs found in the old extension. In this case, you could utilize the library's MongoDB\Database::listCollections() method without worrying about different server versions. If you are curious about how we handle differing server versions, you can take a look at the MongoDB\Operation\ListCollections implementation.
 

By the way I am not very happy with the general usage of Objects in the driver - they are hard to use and often very confusing.
Associative arrays as defaults are way easier to handle in my opinion.

The reasons for the new driver's default behavior is discussed here. Note that in the library, we default to returning BSONDocument and BSONArray classes for BSON documents and arrays, respectively. Those classes both extend PHP's ArrayObject type and allow you to use array access operators, unlike the stdClass instances returned by the raw driver. In the linked article, we also demonstrate how to configure the library to emulate the legacy driver and return PHP arrays for BSON documents and arrays if desired.
 

- Johannes




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/a968be96-73dd-4099-b54c-10484d84d004%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Johannes Reichardt

unread,
Mar 29, 2017, 5:05:37 PM3/29/17
to mongodb-user
Thank you for the insight. I already felt that it has to do with the DB version.
I don't fully understand the problems regarding objects for data but I still do not see a point in returning information like names of indexes as an object by default (posted another question about this before I read your reply).

- Johannes

On Wednesday, March 29, 2017 at 10:54:34 PM UTC+2, Jeremy Mikola wrote:
On Wed, Mar 29, 2017 at 11:15 AM, 'Johannes Reichardt' via mongodb-user <mongod...@googlegroups.com> wrote:
Hi there,

I am fiddeling since hours with no result to retrieve a list of collections. Probably I am overlooking something here.

$manager = new \MongoDB\Driver\Manager($host);
$cmd = new \MongoDB\Driver\Command(['listCollections' => 1]);
$res = $manager->executeCommand($db_name, $cmd);

This gives an error that listCollections is no command but it is listed here. The listCommand (which works) on the other hand doesnt show it.
https://docs.mongodb.com/manual/reference/command/

Should I have to do it differently? The same code with listDatabases works perfectly.

I am using the latest driver for PHP 7.1 on windows that connects to a 2.6 DB on debian.

Any hints on this?

The listCollections command was only introduced in MongoDB 3.0. Earlier versions of the database require you to run a query on the system.namespaces collection. In addition to the extension, we suggest that folks also use the PHP library, which implements many of the high-level APIs found in the old extension. In this case, you could utilize the library's MongoDB\Database::listCollections() method without worrying about different server versions. If you are curious about how we handle differing server versions, you can take a look at the MongoDB\Operation\ListCollections implementation.
 

By the way I am not very happy with the general usage of Objects in the driver - they are hard to use and often very confusing.
Associative arrays as defaults are way easier to handle in my opinion.

The reasons for the new driver's default behavior is discussed here. Note that in the library, we default to returning BSONDocument and BSONArray classes for BSON documents and arrays, respectively. Those classes both extend PHP's ArrayObject type and allow you to use array access operators, unlike the stdClass instances returned by the raw driver. In the linked article, we also demonstrate how to configure the library to emulate the legacy driver and return PHP arrays for BSON documents and arrays if desired.
 

- Johannes




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.

Jeremy Mikola

unread,
Mar 29, 2017, 6:31:03 PM3/29/17
to mongod...@googlegroups.com
On Wed, Mar 29, 2017 at 5:05 PM, 'Johannes Reichardt' via mongodb-user <mongod...@googlegroups.com> wrote:
Thank you for the insight. I already felt that it has to do with the DB version.
I don't fully understand the problems regarding objects for data

If you're referring to not returning BSON documents as PHP arrays, it primarily comes down to the inability to preserve types when roundtripping data to/from the server. To cite the docs I linked earlier:

  • The old driver would convert BSON document {"0": "foo"} and BSON array ["foo"] to the same PHP value: [0 => "foo"]. From there, we can no longer differentiate the original database type. If we were to store that PHP value back into MongoDB, it would end up as a BSON array.
  • The PHP array [0 => "a", 1 => "b"] serializes to a BSON array, as its keys are numeric, start at zero, and advance sequentially. If you unset key "1", the value still serializes as a BSON array; however, unsetting key "0" creates a gap in the sequence and the value serializes as a BSON document. This is true for both the old and new driver.

These problems are not unique to the MongoDB driver. You'll find that json_encode() has the same challenge.


 
but I still do not see a point in returning information like names of indexes as an object by default (posted another question about this before I read your reply).

I'll follow up on the second thread.
 
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages