PHP Velho Oeste 2024

MongoClient クラス

(PECL mongo >=1.3.0)

警告

このクラスを定義している拡張モジュールは非推奨です。 かわりに MongoDB 拡張モジュールを使うべきです。 このクラスの代替として、以下が使えます。

はじめに

PHP と MongoDB の接続を管理します。

このクラスを使って、接続を作ったり管理したりします。典型的な使いかたは、このようになります。

例1 MongoClient の基本的な使いかた

<?php

$m 
= new MongoClient(); // 接続します
$db $m->foo// "foo" というデータベースを取得します

?>

接続の作成に関する詳細な情報は、 MongoClient::__construct() および 接続 のセクションを参照ください。

クラス概要

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) [, array $driver_options ]]] )
public Mongo::close ([ boolean|string $connection ] ) : bool
public Mongo::connect ( void ) : bool
public dropDB ( mixed $db ) : array
public Mongo::__get ( string $dbname ) : MongoDB
public static getConnections ( void ) : array
public Mongo::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 Mongo::__toString ( void ) : string
}

定義済み定数

MongoClient 定数

MongoClient::VERSION
PHP ドライバのバージョン。中間バージョンの場合は最後に "dev" や "+"、そして "-" がつくことがあります。
MongoClient::DEFAULT_HOST
ホストを指定しない場合に接続するホスト。
MongoClient::DEFAULT_PORT
ポートを指定しない場合に接続するポート。
MongoClient::RP_PRIMARY
プライマリのレプリカセットメンバーを 優先読み込み する。
MongoClient::RP_PRIMARY_PREFERRED
プライマリのレプリカセットメンバーのほうを優先して 優先読み込み する。
MongoClient::RP_SECONDARY
セカンダリのレプリカセットメンバーを 優先読み込み する。
MongoClient::RP_SECONDARY_PREFERRED
セカンダリのレプリカセットメンバーの方を優先して 優先読み込み する。
MongoClient::RP_NEAREST
最も近いレプリカセットメンバーを 優先読み込み する。

フィールド

connected

このプロパティに TRUE が設定されるのは、データベース接続がオープンしている場合です。 それ以外の場合は FALSE が設定されます。 レプリカセットへの接続の場合は、現在の優先読み込みにマッチするノードに接続している場合にのみ TRUE となります。 このプロパティは、認証を考慮しません。

このプロパティは、バージョン 1.5.0 以降で 非推奨 となりました。

status

このプロパティはもう使われておらず、値は NULL に設定されます。 バージョン 1.1.x より前のドライバでは、持続的接続を使う場合にここに文字列 ("recycled""new" など) が設定されていました。

このプロパティは、バージョン 1.5.0 以降で 非推奨 となりました。

参考

目次

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
7 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
9 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