PHP 7.1.24 Released

The MongoDB\Driver\Manager class

(mongodb >=1.0.0)


The MongoDB\Driver\Manager is the main entry point to the extension. It is responsible for maintaining connections to MongoDB (be it standalone server, replica set, or sharded cluster).

No connection to MongoDB is made upon instantiating the Manager. This means the MongoDB\Driver\Manager can always be constructed, even though one or more MongoDB servers are down.

Any write or query can throw connection exceptions as connections are created lazily. A MongoDB server may also become unavailable during the life time of the script. It is therefore important that all actions on the Manager to be wrapped in try/catch statements.

Class synopsis

final MongoDB\Driver\Manager {
/* Methods */
final public __construct ([ string $uri = "mongodb://" [, array $uriOptions = array() [, array $driverOptions = array() ]]] )
final public MongoDB\Driver\WriteResult executeBulkWrite ( string $namespace , MongoDB\Driver\BulkWrite $bulk [, array $options = array() ] )
final public MongoDB\Driver\Cursor executeCommand ( string $db , MongoDB\Driver\Command $command [, array $options = array() ] )
final public MongoDB\Driver\Cursor executeQuery ( string $namespace , MongoDB\Driver\Query $query [, array $options = array() ] )
final public MongoDB\Driver\Cursor executeReadCommand ( string $db , MongoDB\Driver\Command $command [, array $options = array() ] )
final public MongoDB\Driver\Cursor executeReadWriteCommand ( string $db , MongoDB\Driver\Command $command [, array $options = array() ] )
final public MongoDB\Driver\Cursor executeWriteCommand ( string $db , MongoDB\Driver\Command $command [, array $options = array() ] )
final public MongoDB\Driver\ReadConcern getReadConcern ( void )
final public MongoDB\Driver\ReadPreference getReadPreference ( void )
final public array getServers ( void )
final public MongoDB\Driver\WriteConcern getWriteConcern ( void )
final public MongoDB\Driver\Server selectServer ( MongoDB\Driver\ReadPreference $readPreference )
final public MongoDB\Driver\Session startSession ([ array $options ] )


Example #1 MongoDB\Driver\Manager::__construct() basic example

var_dump()ing a MongoDB\Driver\Manager will print out various details about the manager that are otherwise not normally exposed. This can be useful to debug how the driver views your MongoDB setup, and which options are used.


= new MongoDB\Driver\Manager("mongodb://localhost:27017");


The above example will output something similar to:

object(MongoDB\Driver\Manager)#1 (3) {
  string(25) "mongodb://localhost:27017"
  array(13) {
    string(6) "direct"
    string(4) "born"
    string(25) "mongodb://localhost:27017"
    array(0) {

Table of Contents

add a note add a note

User Contributed Notes 2 notes

mike at eastghost dot com
2 months ago
According to Mongo, this (i.e., MongoDB\Driver\Manager) is an "entry point" for the extension:

"This class serves as an entry point for the MongoDB PHP Library. It is the preferred class for connecting to a MongoDB server or cluster of servers and acts as a gateway for accessing individual databases and collections. MongoDB\Client is analogous to the driver’s MongoDB\Driver\Manager class, which it composes."

copied from here:

However, any comparison of the "mongodb" docs here on versus the "mongodb driver" docs on mongo's site shows dramatic and ever-changing differences.
5 months ago
It's worth noting that Manager is final, so you cannot mock it easily with tools like PhpUnit.
To Top