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.
$class = 'MongoClient';
$class = 'Mongo';
$conn = new $class($hosts, $args);
The MongoClient class
(PECL mongoclient >=1.3.0)
A connection between PHP and MongoDB.
This class is used to create and manage connections. A typical use is:
Example #1 MongoClient basic usage
$m = new MongoClient(); // connect
$db = $m->foo; // get the database named "foo"
/* 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 =
public string $status =
protected string $server =
protected boolean $persistent =
/* Methods */
public __construct ([ string
$server= "mongodb://localhost:27017" [, array
$options= array("connect" =>
TRUE) ]] )
public bool close ([ boolean|string
public bool connect ( void )
public MongoDB __get ( string
public static void getConnections ( void )
public array getHosts ( void )
public array getReadPreference ( void )
public array listDBs ( void )
public MongoCollection selectCollection ( string
public MongoDB selectDB ( string
public bool setReadPreference ( string
public string __toString ( void )}
- PHP driver version. May be suffixed with "+" 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
TRUEif we have a open connection the database based on the ReadPreference and tagsets (for ReplicaSet connections),
FALSEotherwise. This property does not take authentication into account.
If this is a persistent connection, if the connection was created for
this object or is being reused. If this is not a persistent connection,
this field should be
Table of Contents
- MongoClient::close — Closes this connection
- MongoClient::connect — Connects to a database server
- MongoClient::__construct — Creates a new database connection object
- MongoClient::dropDB — Drops a database [deprecated]
- MongoClient::__get — Gets a database
- MongoClient::getConnections — Return info about all open connections
- MongoClient::getHosts — Updates status for all associated hosts
- MongoClient::getReadPreference — Get the read preference for this connection
- MongoClient::listDBs — Lists all of the databases available.
- MongoClient::selectCollection — Gets a database collection
- MongoClient::selectDB — Gets a database
- MongoClient::setReadPreference — Set the read preference for this connection
- MongoClient::__toString — String representation of this connection
add a note User Contributed Notes MongoClient - [3 notes]
jazz at funkynerd dot com ¶
5 months ago
mike at eastghost dot com ¶
2 months 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 ¶
2 months ago
php monogo driver 1.3.4
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/pri.eastghost.com)
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.