PHP Class MongoClient

Inheritance: use trait Alcaeus\MongoDbAdapter\Helper\ReadPreference, use trait Alcaeus\MongoDbAdapter\Helper\WriteConcern
Show file Open project: koubas/mongofill Class Usage Examples

Public Properties

Property Type Description
$boolean string
$connected boolean
$persistent boolean
$protocols array
$status string

Public Methods

Method Description
__construct ( string $server = 'mongodb://localhost:27017', array $options = ['connect' => true] ) Creates a new database connection object
__get ( string $dbname ) : MongoDB Gets a database
__toString ( ) : string String representation of this connection
_getAuthenticationDatabase ( ) : string
_getAuthenticationPassword ( ) : string
_getAuthenticationUsername ( ) : string
_getOption ( string $name ) : string
_getReadProtocol ( array $readPreference = [] )
_getWriteProtocol ( )
_validateReadPreference ( $readPreference, array $tags = null )
close ( boolean | string $connection = null ) : boolean Closes this connection
connect ( ) : boolean Connects to a database server or replica set
dropDB ( mixed $db ) : array Drops a database [deprecated]
getConnections ( ) : array Return info about all open connections
getHosts ( ) : array Updates status for all associated hosts
getReadPreference ( ) : array Get the read preference for this connection
killCursor ( string $serverHash, integer | mongoint64 $id ) : boolean Kills a specific cursor on the server
listDBs ( ) : array Lists all of the databases available.
selectCollection ( string $db, string $collection ) : MongoCollection Gets a database collection
selectDB ( string $name ) : MongoDB Gets a database
setReadPreference ( string $readPreference, array $tags = null ) : boolean Set the read preference for this connection

Protected Methods

Method Description
parseHostString ( $host_str ) : array Given a host string, validate and parse it into hostname and port parts
purgeAllProtocols ( ) Disconnect and remove all protocols/sockets
purgeProtocols ( array $protocols ) Disconnect and remove given list of protocols
retrieveReplSetHosts ( ) For a replset, we need to connect to the first active host in our seed list and then ask the replset for its list of valid hosts. This list will be pre-filtered of any hidden members including arbiters. This is the host list we will use moving forward.

Private Methods

Method Description
connectToFirstAvailableHost ( ) : Mongofill/Protocol Iterates through configured hosts array attempting to connect to each. Returns as soon as a successful connection is made. If no connection can be established, throw an exception.
connectToHost ( $host, $port ) : Mongofill\Protocol Establish a connection to specified host if not already connected to it
connectToReplSet ( ) Attempts to connect to the MongoDB replSet and retrieve the replSet status, updating the host list with the proper host list from the replSet isMaster() command.
connectToReplSetPrimary ( ) : Mongofill/Protocol Returns a connection the PRIMARY member of the current host list.
getNearestHostProtocol ( array $allowedServerTypes, array $readPreference ) : Mongofill\Protocol Get a Protocol for the nearest candidate server matching the given types and read preference. This implements Member Selection as described here: http://docs.mongodb.org/manual/core/read-preference-mechanics/#replica-set-read-preference-behavior-member-selection
getReplSetInfo ( ) : array Get an array with replica set config and status for the current PRIMARY.
pingHost ( $host, $port )

Method Details

__construct() public method

Creates a new database connection object
public __construct ( string $server = 'mongodb://localhost:27017', array $options = ['connect' => true] )
$server string - The server name. server should have the form: mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db
$options array - An array of options for the connection.

__get() public method

Gets a database
public __get ( string $dbname ) : MongoDB
$dbname string - The database name.
return MongoDB - Returns a new db object.

__toString() public method

String representation of this connection
public __toString ( ) : string
return string - Returns hostname and port for this connection.

_getAuthenticationDatabase() public method

public _getAuthenticationDatabase ( ) : string
return string - The name of the database to authenticate

_getAuthenticationPassword() public method

public _getAuthenticationPassword ( ) : string
return string - The password for authentication

_getAuthenticationUsername() public method

public _getAuthenticationUsername ( ) : string
return string - The username for authentication

_getOption() public method

public _getOption ( string $name ) : string
$name string - The option name.
return string - The option value

_getReadProtocol() public method

public _getReadProtocol ( array $readPreference = [] )
$readPreference array

_getWriteProtocol() public method

public _getWriteProtocol ( )

_validateReadPreference() public static method

public static _validateReadPreference ( $readPreference, array $tags = null )
$tags array

close() public method

Closes this connection
public close ( boolean | string $connection = null ) : boolean
$connection boolean | string - If connection is not given, or FALSE then connection that would be selected for writes would be closed. In a single-node configuration, that is then the whole connection, but if you are connected to a replica set, close() will only close the connection to the primary server.
return boolean - Returns if the connection was successfully closed.

connect() public method

Connects to a database server or replica set
public connect ( ) : boolean
return boolean - If the connection was successful.

dropDB() public method

Drops a database [deprecated]
public dropDB ( mixed $db ) : array
$db mixed - The database to drop. Can be a MongoDB object or the name of the database.
return array - Returns the database response.

getConnections() public static method

Return info about all open connections
public static getConnections ( ) : array
return array - An array of open connections.

getHosts() public method

Updates status for all associated hosts
public getHosts ( ) : array
return array - Returns an array of information about the hosts in the set. Includes each host's hostname, its health (1 is healthy), its state (1 is primary, 2 is secondary, 0 is anything else), the amount of time it took to ping the server, and when the last ping occurred.

getReadPreference() public method

Get the read preference for this connection
public getReadPreference ( ) : array
return array -

killCursor() public method

Kills a specific cursor on the server
public killCursor ( string $serverHash, integer | mongoint64 $id ) : boolean
$serverHash string - The server hash that has the cursor. This can be obtained through MongoCursor::info.
$id integer | mongoint64 - The ID of the cursor to kill. You can either supply an int containing the 64 bit cursor ID, or an object of the MongoInt64 class. The latter is necessary on 32 bit platforms (and Windows).
return boolean - Returns TRUE if the method attempted to kill a cursor, and FALSE if there was something wrong with the arguments (such as a wrong server_hash). The return status does not reflect where the cursor was actually killed as the server does not provide that information.

listDBs() public method

Lists all of the databases available.
public listDBs ( ) : array
return array - Returns an associative array containing three fields. The first field is databases, which in turn contains an array. Each element of the array is an associative array corresponding to a database, giving th database's name, size, and if it's empty. The other two fields are totalSize (in bytes) and ok, which is 1 if this method ran successfully.

parseHostString() protected method

Given a host string, validate and parse it into hostname and port parts
protected parseHostString ( $host_str ) : array
$host_str string - The host:port string
return array - In the form ['host' => $host, 'port' => $port, 'hash' => "$host:$port"]

purgeAllProtocols() protected method

Disconnect and remove all protocols/sockets
protected purgeAllProtocols ( )

purgeProtocols() protected method

Disconnect and remove given list of protocols
protected purgeProtocols ( array $protocols )
$protocols array - list of protocol objects to purge

retrieveReplSetHosts() protected method

For a replset, we need to connect to the first active host in our seed list and then ask the replset for its list of valid hosts. This list will be pre-filtered of any hidden members including arbiters. This is the host list we will use moving forward.
protected retrieveReplSetHosts ( )

selectCollection() public method

Gets a database collection
public selectCollection ( string $db, string $collection ) : MongoCollection
$db string - The database name.
$collection string - The collection name.
return MongoCollection - Returns a new collection object.

selectDB() public method

Gets a database
public selectDB ( string $name ) : MongoDB
$name string - The database name.
return MongoDB - Returns a new database object.

setReadPreference() public method

Set the read preference for this connection
public setReadPreference ( string $readPreference, array $tags = null ) : boolean
$readPreference string -
$tags array -
return boolean -

Property Details

$boolean public property

public string $boolean
return string

$connected public property

public bool $connected
return boolean

$persistent public property

public bool $persistent
return boolean

$protocols public property

public array $protocols
return array

$status public property

public string $status
return string