Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

The MongoClient class

(PECL mongoclient >=1.3.0)


A connection manager for PHP and MongoDB.

This class is used to create and manage connections. A typical use is:

Example #2 MongoClient basic usage


= new MongoClient(); // connect
$db $m->foo// get the database named "foo"


See MongoClient::__construct() and the section on connecting for more information about creating connections.

Class synopsis

MongoClient {
/* Constants */
const string VERSION ;
const string DEFAULT_HOST = "localhost" ;
const int DEFAULT_PORT = 27017 ;
const string RP_PRIMARY = "primary" ;
const string RP_PRIMARY_PREFERRED = "primaryPreferred" ;
const string RP_SECONDARY = "secondary" ;
const string RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
const string RP_NEAREST = "nearest" ;
/* Properties */
public boolean $connected = FALSE ;
public string $status = NULL ;
protected string $server = NULL ;
protected boolean $persistent = NULL ;
/* Methods */
public __construct ([ string $server = "mongodb://localhost:27017" [, array $options = array("connect" => TRUE) ]] )
public bool close ([ boolean|string $connection ] )
public bool connect ( void )
public array dropDB ( mixed $db )
public MongoDB __get ( string $dbname )
public static array getConnections ( void )
public array getHosts ( void )
public array getReadPreference ( void )
public bool killCursor ( string $server_hash , int|MongoInt64 $id )
public array listDBs ( void )
public MongoCollection selectCollection ( string $db , string $collection )
public MongoDB selectDB ( string $name )
public bool setReadPreference ( string $read_preference [, array $tags ] )
public string __toString ( void )

Predefined Constants

MongoClient Constants

PHP driver version. May be suffixed with "dev", "+" or "-" if it is in-between versions.
Host to connect to if no host is given.
Port to connect to if no port is given.
Read preference for the primary replica set member.
Read preference for preferring the primary replica set member.
Read preference for a secondary replica set member.
Read preference for preferring a secondary replica set member.
Read preference for the nearest replica set member.


This property will be set to TRUE if we have a open connection the database based on the ReadPreference and tagsets (for ReplicaSet connections), FALSE otherwise. This property does not take authentication into account.
This property is no longer used and will be set to NULL In driver versions 1.1.x and earlier, this may be set to a string value (e.g. "recycled", "new") when persistent connections are used.

Table of Contents

PHP Manual