MongoClient 类
(PECL mongo >=1.3.0)
Warning
This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:
简介
PHP 和 MongoDB 的连接管理器。
这个类用于创建和管理连接。典型的用法:
Example #1 MongoClient 基本用法
<?php
$m = new MongoClient(); // 连接
$db = $m->foo; // 获取名称为 "foo" 的数据库
?>
关于创建连接的更多信息,参见 MongoClient::__construct() 和 connecting 的章节。
类摘要
MongoClient
{
/* 常量 */
/* 属性 */
protected
string
$server
=
NULL
;
protected
boolean
$persistent
=
NULL
;/* 方法 */
public __construct
([ string
}$server
= "mongodb://localhost:27017"
[, array $options
= array("connect" => TRUE
)
]] )预定义常量
MongoClient 常量
MongoClient::VERSION
- PHP 驱动版本。有可能附加 "dev","+" 或 "-" 如果是在两个版本之间。
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"
- 副本集最近节点的读取选项。
字段属性
- connected
-
如果我们有一个打开的数据库连接,将会被设置为
TRUE
,否则是FALSE
。 如果连接副本集(replica set)里一个节点并匹配当前的读取选项 ,该属性仅会是TRUE
。 这个属性不考虑账户是否已认证。版本 1.5.0 后该属性已经废弃( deprecated)。
- status
-
这个属性不会再被使用,将会被设置为
NULL
在驱动版本 1.1.x 及更早版本中,使用持久连接时这可能会被设置为字符串的值(比如 "recycled", "new")。版本 1.5.0 后该属性已经废弃( deprecated)。
参见
- 读取首选项
- Write Concerns
- 链接服务器
- 关于 » connecting 的 MongoDB 核心文档
Table of Contents
- MongoClient::close — 关闭连接
- MongoClient::connect — 连接到数据库服务器
- MongoClient::__construct — 创建一个新的数据库连接对象
- MongoClient::dropDB — 删除一个数据库 [已废弃]
- MongoClient::__get — 取得一个数据库
- MongoClient::getConnections — 返回所有已打开连接的信息
- MongoClient::getHosts — 更新所有关联主机的状态信息
- MongoClient::getReadPreference — 获取此连接的读取首选项
- MongoClient::getWriteConcern — Get the write concern for this connection
- MongoClient::killCursor — Kills a specific cursor on the server
- MongoClient::listDBs — 列出所有有效数据库
- MongoClient::selectCollection — 获取数据库的文档集
- MongoClient::selectDB — 获取一个数据库
- MongoClient::setReadPreference — 为该连接设置读取选项
- MongoClient::setWriteConcern — Set the write concern for this connection
- MongoClient::__toString — 该连接的字符串表达方式

User Contributed Notes 5 notes
mike at eastghost dot com ¶
6 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 ¶
6 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/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.
East Ghost Com ¶
2 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 ¶
5 years ago
Using the 1.2.5-5.5 vc11 driver the connected attribute is depracted.
jazz at funkynerd dot com ¶
6 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.
<?php
$class = 'MongoClient';
if(!class_exists($class)){
$class = 'Mongo';
}
$conn = new $class($hosts, $args);
?>