核心类
在线手册:中文  英文

MongoClient 类

(PECL mongoclient >=1.3.0)

简介

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 bool close ([ boolean|string $connection ] )
public bool connect ( void )
public array dropDB ( mixed $db )
public MongoDB __get ( string $dbname )
public static void getConnections ( void )
public array getHosts ( void )
public array getReadPreference ( void )
public array listDBs ( void )
public MongoCollection selectCollection ( string $db , string $collection )
public MongoDB selectDB ( string $name )
public bool setReadPreference ( string $read_preference [, array $tags ] )
public string __toString ( void )
}

预定义常量

MongoClient 常量

MongoClient::VERSION
PHP 驱动版本。有可能附加 "+" 或 "-" 如果是在两个版本之间。
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。 这个属性不考虑账户是否已认证。
status
是否为持久连接,创建的连接能够在此对象中复用。如果不是持久连接,这个字段会是 NULL

参见

Table of Contents


核心类
在线手册:中文  英文

用户评论:

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

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.

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);
?>

易百教程