class Connection (View source)

Wrapper for the MongoClient class.

Properties

protected MongoClient $mongoClient The PHP MongoClient instance being wrapped.
protected string $server Server string used to construct the MongoClient instance (optional).
protected array $options Options used to construct the MongoClient instance (optional).
protected array $driverOptions Driver options used to construct the MongoClient instance (optional).
protected Configuration $config The Configuration for this connection.
protected EventManager $eventManager The EventManager used to dispatch events.

Methods

__construct(string|MongoClient $server = null, array $options = [], Configuration $config = null, EventManager $evm = null, array $driverOptions = [])

Constructor.

boolean
close()

Wrapper method for MongoClient::close().

boolean
connect()

Wrapper method for MongoClient::connect().

array
dropDatabase(string $database)

Wrapper method for MongoClient::dropDB().

getConfiguration()

Get the Configuration used by this Connection.

EventManager
getEventManager()

Get the EventManager used by this Connection.

MongoClient
getMongo() deprecated

Get the MongoClient instance being wrapped.

setMongo(MongoClient $mongoClient) deprecated

Set the MongoClient instance to wrap.

MongoClient
getMongoClient()

Get the MongoClient instance being wrapped.

array
getReadPreference()

Wrapper method for MongoClient::getReadPreference().

boolean
setReadPreference(string $readPreference, array $tags = null)

Wrapper method for MongoClient::setReadPreference().

string|null
getServer()

Get the server string.

string
getStatus() deprecated

Gets the $status property of the wrapped MongoClient instance.

initialize()

Construct the wrapped MongoClient instance if necessary.

boolean
isConnected()

Checks whether the connection is initialized and connected.

array
listDatabases()

Wrapper method for MongoClient::listDBs().

log(array $log)

Log something using the configured logger callable (if available).

selectCollection(string $db, string $collection)

Wrapper method for MongoClient::selectCollection().

selectDatabase(string $name)

Wrapper method for MongoClient::selectDatabase().

MongoDB
__get(string $database)

Wrapper method for MongoClient::__get().

string
__toString()

Wrapper method for MongoClient::__toString().

doSelectDatabase(string $name)

Return a new Database instance.

mixed
retry(Closure $retry)

Conditionally retry a closure if it yields an exception.

array
convertConnectTimeout(array $options)

Converts "timeout" MongoClient constructor option to "connectTimeoutMS" for driver versions 1.4.0+.

array
convertWriteTimeout(array $options)

Converts "wTimeout" MongoClient constructor option to "wTimeoutMS" for driver versions 1.4.0+.

Details

__construct(string|MongoClient $server = null, array $options = [], Configuration $config = null, EventManager $evm = null, array $driverOptions = [])

Constructor.

If $server is an existing MongoClient instance, the $options parameter will not be used.

Parameters

string|MongoClient $server Server string or MongoClient instance
array $options MongoClient constructor options
Configuration $config Configuration instance
EventManager $evm EventManager instance
array $driverOptions MongoClient constructor options

boolean close()

Wrapper method for MongoClient::close().

Return Value

boolean

See also

http://php.net/manual/en/mongoclient.close.php

boolean connect()

Wrapper method for MongoClient::connect().

Return Value

boolean

See also

http://php.net/manual/en/mongoclient.connect.php

array dropDatabase(string $database)

Wrapper method for MongoClient::dropDB().

This method will dispatch preDropDatabase and postDropDatabase events.

Parameters

string $database

Return Value

array

See also

http://php.net/manual/en/mongoclient.dropdb.php

Configuration getConfiguration()

Get the Configuration used by this Connection.

Return Value

Configuration

EventManager getEventManager()

Get the EventManager used by this Connection.

Return Value

EventManager

MongoClient getMongo() deprecated

deprecated 1.1 Replaced by getMongoClient(); will be removed for 2.0

Get the MongoClient instance being wrapped.

Return Value

MongoClient

setMongo(MongoClient $mongoClient) deprecated

deprecated 1.1 Will be removed for 2.0

Set the MongoClient instance to wrap.

Parameters

MongoClient $mongoClient

MongoClient getMongoClient()

Get the MongoClient instance being wrapped.

Return Value

MongoClient

array getReadPreference()

Wrapper method for MongoClient::getReadPreference().

For driver versions between 1.3.0 and 1.3.3, the return value will be converted for consistency with {@link Connection::setReadPreference()}.

boolean setReadPreference(string $readPreference, array $tags = null)

Wrapper method for MongoClient::setReadPreference().

Parameters

string $readPreference
array $tags

Return Value

boolean

See also

http://php.net/manual/en/mongoclient.setreadpreference.php

string|null getServer()

Get the server string.

Return Value

string|null

string getStatus() deprecated

deprecated 1.1 No longer used in driver; Will be removed for 1.2

Gets the $status property of the wrapped MongoClient instance.

Return Value

string

initialize()

Construct the wrapped MongoClient instance if necessary.

This method will dispatch preConnect and postConnect events.

boolean isConnected()

Checks whether the connection is initialized and connected.

Return Value

boolean

array listDatabases()

Wrapper method for MongoClient::listDBs().

log(array $log)

Log something using the configured logger callable (if available).

Parameters

array $log

Collection selectCollection(string $db, string $collection)

Wrapper method for MongoClient::selectCollection().

Parameters

string $db
string $collection

Return Value

Collection

See also

http://php.net/manual/en/mongoclient.selectcollection.php

Database selectDatabase(string $name)

Wrapper method for MongoClient::selectDatabase().

This method will dispatch preSelectDatabase and postSelectDatabase events.

Parameters

string $name

Return Value

Database

See also

http://php.net/manual/en/mongoclient.selectdatabase.php

MongoDB __get(string $database)

Wrapper method for MongoClient::__get().

Parameters

string $database

Return Value

MongoDB

See also

http://php.net/manual/en/mongoclient.get.php

string __toString()

Wrapper method for MongoClient::__toString().

protected Database doSelectDatabase(string $name)

Return a new Database instance.

If a logger callable was defined, a LoggableDatabase will be returned.

Parameters

string $name

Return Value

Database

See also

Connection::selectDatabase

protected mixed retry(Closure $retry)

Conditionally retry a closure if it yields an exception.

If the closure does not return successfully within the configured number of retries, its first exception will be thrown.

Parameters

Closure $retry

Return Value

mixed

protected array convertConnectTimeout(array $options)

Converts "timeout" MongoClient constructor option to "connectTimeoutMS" for driver versions 1.4.0+.

Note: MongoClient actually allows case-insensitive option names, but we'll only process the canonical version here.

Parameters

array $options

Return Value

array

protected array convertWriteTimeout(array $options)

Converts "wTimeout" MongoClient constructor option to "wTimeoutMS" for driver versions 1.4.0+.

Note: MongoClient actually allows case-insensitive option names, but we'll only process the canonical version here.

Parameters

array $options

Return Value

array