©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PECL mongo >=1.3.0)
PHP 和 MongoDB 的连接管理器。
这个类用于创建和管理连接。典型的用法:
Example #1 MongoClient 基本用法
<?php
$m = new MongoClient (); // 连接
$db = $m -> foo ; // 获取名称为 "foo" 的数据库
?>
关于创建连接的更多信息,参见 MongoClient::__construct() 和 connecting 的章节。
FALSE
;NULL
;NULL
;NULL
;$server
= "mongodb://localhost:27017"
[, array $options
= array("connect" => TRUE
)
]] )$connection
] )$db
)$dbname
)$server_hash
, int|MongoInt64 $id
)$db
, string $collection
)$name
)$read_preference
[, array $tags
] )$w
[, int $wtimeout
] )MongoClient::VERSION
MongoClient::DEFAULT_HOST
"localhost"
MongoClient::DEFAULT_PORT
27017
MongoClient::RP_PRIMARY
"primary"
MongoClient::RP_PRIMARY_PREFERRED
"primaryPreferred"
MongoClient::RP_SECONDARY
"secondary"
MongoClient::RP_SECONDARY_PREFERRED
"secondaryPreferred"
MongoClient::RP_NEAREST
"nearest"
TRUE
,否则是 FALSE
。
这个属性不考虑账户是否已认证。
NULL
在驱动版本 1.1.x 及更早版本中,使用持久连接时这可能会被设置为字符串的值(比如 "recycled", "new")。
[#1] bennettsst at NOSPAM dot gmail dot com [2014-05-12 11:20:30]
Using the 1.2.5-5.5 vc11 driver the connected attribute is depracted.
[#2] mike at eastghost dot com [2013-03-07 09:35:54]
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
[#3] mike at eastghost dot com [2013-03-07 09:34:24]
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.
[#4] jazz at funkynerd dot com [2012-11-28 01:36:46]
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);
?>