The MongoClient class

(PECL mongo >=1.3.0)


This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:


A connection manager for PHP and MongoDB.

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

Example #1 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 bool $connected = false ;
public string $status = null ;
protected string $server = null ;
protected bool $persistent = null ;
/* Methods */
public __construct ( string $server = "mongodb://localhost:27017" , array $options = array("connect" => true) , array $driver_options = ? )
public close ( bool|string $connection = ? ) : bool
public connect ( ) : bool
public dropDB ( mixed $db ) : array
public __get ( string $dbname ) : MongoDB
public static getConnections ( ) : array
public getHosts ( ) : array
public getReadPreference ( ) : array
public getWriteConcern ( ) : array
public killCursor ( string $server_hash , int|MongoInt64 $id ) : bool
public listDBs ( ) : array
public selectCollection ( string $db , string $collection ) : MongoCollection
public selectDB ( string $name ) : MongoDB
public setReadPreference ( string $read_preference , array $tags = ? ) : bool
public setWriteConcern ( mixed $w , int $wtimeout = ? ) : bool
public __toString ( ) : string

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 to the database, false otherwise. If the connection is to a replica set, this property will only be true if the driver has a connection to a node matching the current read preference. This property does not take authentication into account.

This property is deprecated since version 1.5.0.


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.

This property is deprecated since version 1.5.0.

Table of Contents

add a note add a note

User Contributed Notes 5 notes

mike at eastghost dot com
8 years ago
This will help maintain sanity while debugging replicaSet connectivity problems:

error_reporting( E_ALL )
// print every log message possible
\MongoLog::setLevel(\MongoLog::ALL); // all log levels
\MongoLog::setModule(\MongoLog::ALL); // all parts of the driver
mike at eastghost dot com
8 years ago
php monogo driver 1.3.4
feb 2013

After demoting old replicaset primary to secondary, and promoting old replicaset second into primary, we began seeing "No candidate servers found" MongoException at initial attempt to connect to (new) replicaset primary (via this hint in the /etc/mongo.conf: replSet = rs1/

Fix seems to be

1. NEVER list "localhost" in the bind= line of /etc/mongo.conf

2. ALWAYS list every replica set member in every member's /etc/hosts file -- there seems to be something wrong with DNS lookup timing.
East Ghost Com
4 years ago
One MongoClient is required per every MongoDB.

So, if you have a website-specific database but also a shared database (like maybe one holding ZipCodes and State names), then each needs its own MongoClient.  One MongoClient can not be shared amongst multiple MongoDB's.
bennettsst at NOSPAM dot gmail dot com
7 years ago
Using the 1.2.5-5.5 vc11 driver the connected attribute is depracted.
jazz at funkynerd dot com
8 years ago
Seeing as the Mongo class has been deprecated, I'm using the following code to allow compatibility with the pre 1.3.0 driver successfully.

= 'MongoClient';

$class = 'Mongo';
$conn = new $class($hosts, $args);
To Top