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 {
/* 常量 */
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" ;
/* 属性 */
public boolean connected = FALSE ;
public string status = NULL ;
protected string server = NULL ;
protected boolean persistent = NULL ;
/* 方法 */
public __construct ([ string $server = "mongodb://localhost:27017" [, array $options = array("connect" => TRUE) ]] )
public close ([ boolean|string $connection ] ) : bool
public connect ( void ) : bool
public dropDB ( mixed $db ) : array
public __get ( string $dbname ) : MongoDB
public static getConnections ( void ) : array
public getHosts ( void ) : array
public getReadPreference ( void ) : array
public getWriteConcern ( void ) : array
public killCursor ( string $server_hash , int|MongoInt64 $id ) : bool
public listDBs ( void ) : 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 ( void ) : string
}

预定义常量

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)。

参见

Table of Contents

add a note add a note

User Contributed Notes 5 notes

up
8
mike at eastghost dot com
11 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
up
5
mike at eastghost dot com
11 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.
up
-1
East Ghost Com
8 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.
up
-3
bennettsst at NOSPAM dot gmail dot com
10 years ago
Using the 1.2.5-5.5 vc11 driver the connected attribute is depracted.
up
-29
jazz at funkynerd dot com
11 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);
?>
To Top