The MongoClient class
(PECL mongo >=1.3.0)
Introduction
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
<?php
$m = 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 */
/* Properties */
protected
string
$server
=
NULL
;
protected
boolean
$persistent
=
NULL
;/* Methods */
public __construct
([ string
}$server
= "mongodb://localhost:27017"
[, array $options
= array("connect" => TRUE
)
[, array $driver_options
]]] )Predefined Constants
MongoClient Constants
MongoClient::VERSION
- PHP driver version. May be suffixed with "dev", "+" or "-" if it is in-between versions.
MongoClient::DEFAULT_HOST
-
"localhost"
- Host to connect to if no host is given.
MongoClient::DEFAULT_PORT
-
27017
- Port to connect to if no port is given.
MongoClient::RP_PRIMARY
-
"primary"
- Read preference for the primary replica set member.
MongoClient::RP_PRIMARY_PREFERRED
-
"primaryPreferred"
- Read preference for preferring the primary replica set member.
MongoClient::RP_SECONDARY
-
"secondary"
- Read preference for a secondary replica set member.
MongoClient::RP_SECONDARY_PREFERRED
-
"secondaryPreferred"
- Read preference for preferring a secondary replica set member.
MongoClient::RP_NEAREST
-
"nearest"
- Read preference for the nearest replica set member.
Fields
- connected
-
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. - status
-
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.
See Also
- Read Preferences
- Write Concerns
- Connecting
- MongoDB core docs on » connecting
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::getWriteConcern — Get the write concern for this connection
- MongoClient::killCursor — Kills a specific cursor on the server
- 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::setWriteConcern — Set the write concern for this connection
- MongoClient::__toString — String representation of this connection
Коментарии
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.
<?php
$class = 'MongoClient';
if(!class_exists($class)){
$class = 'Mongo';
}
$conn = new $class($hosts, $args);
?>
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/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.
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
Using the 1.2.5-5.5 vc11 driver the connected attribute is depracted.
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.