Retrieve MongoDB\Client from \MongoDB\Driver\Manager

Viewed 22

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\Client.

The documentation states

MongoDB\Client is analogous to the driver’s MongoDB\Driver\Manager class, which it composes.

How do I list databases from an instance of \MongoDB\Driver\Manager ?

1 Answers

If you are using the driver (i.e. the ext-mongodb extension) directly, you don't have the high-level API that the PHP library (which contains MongoDB\Client) provides. In this case, you'll have to run the command manually through the manager.

I would recommend using the MongoDB library instead of only using the PHP extension. The extension wraps the C driver and thus only provides a low-level API, while the common drivers API is implemented in the PHP library. This will make working with documents significantly easier.

Related