PHP Velho Oeste 2024

MongoDB クラス

(PECL mongo >=0.9.0)

はじめに

このクラスのインスタンスを使用してデータベースとのやりとりを行います。 データベースを取得するには、このようにします。

例1 データベースの選択

<?php

$m 
= new MongoClient(); // 接続
$db $m->selectDB("example");

?>
データベース名には、ASCII の範囲内でほとんどの文字を使うことができます。 しかし、" " や "." を使うことはできず、空文字列にすることもできません。 "system" という名前も予約済みで、使うことができません。

あまり一般的ではありませんが、 "null"、"[x,y]"、"3"、"\""、"/" などは正しい形式のデータベース名です。

コレクション名とは異なり、データベース名には "$" を含めてもかまいません。

クラス概要

MongoDB {
/* 定数 */
const int PROFILING_OFF = 0 ;
const int PROFILING_SLOW = 1 ;
const int PROFILING_ON = 2 ;
/* フィールド */
public integer w = 1 ;
public integer wtimeout = 10000 ;
/* メソッド */
public authenticate ( string $username , string $password ) : array
public command ( array $command [, array $options = array() [, string &$hash ]] ) : array
public __construct ( MongoClient $conn , string $name )
public createCollection ( string $name [, array $options ] ) : MongoCollection
public createDBRef ( string $collection , mixed $document_or_id ) : array
public drop ( void ) : array
public dropCollection ( mixed $coll ) : array
public execute ( mixed $code [, array $args = array() ] ) : array
public forceError ( void ) : bool
public __get ( string $name ) : MongoCollection
public getCollectionInfo ([ array $options = array() ] ) : array
public getCollectionNames ([ array $options = array() ] ) : array
public getDBRef ( array $ref ) : array
public getGridFS ([ string $prefix = "fs" ] ) : MongoGridFS
public getProfilingLevel ( void ) : int
public getReadPreference ( void ) : array
public getSlaveOkay ( void ) : bool
public getWriteConcern ( void ) : array
public lastError ( void ) : array
public listCollections ([ array $options = array() ] ) : array
public prevError ( void ) : array
public repair ([ bool $preserve_cloned_files = FALSE [, bool $backup_original_files = FALSE ]] ) : array
public resetError ( void ) : array
public selectCollection ( string $name ) : MongoCollection
public setProfilingLevel ( int $level ) : int
public setReadPreference ( string $read_preference [, array $tags ] ) : bool
public setSlaveOkay ([ bool $ok = TRUE ] ) : bool
public setWriteConcern ( mixed $w [, int $wtimeout ] ) : bool
public __toString ( void ) : string
}

定義済み定数

MongoDB ログレベル

MongoDB::PROFILING_OFF
プロファイリングをオフにします。
MongoDB::PROFILING_SLOW
襲い接続 (>100 ms) に対するプロファイリングをオンにします。
MongoDB::PROFILING_ON
すべての操作に対するプロファイリングをオンにします。

フィールド

w
1

成功を返す前に変更をレプリケートするサーバーの数。このクラスの派生クラスである MongoCollection に継承されます。 w の機能が使えるのは、バージョン 1.5.1 以降の MongoDB サーバーと バージョン 1.0.8 以降のドライバを使っている場合のみです。

w は、通知レベルを調整したい場合に使います (MongoCollection::insert()MongoCollection::update()MongoCollection::remove()MongoCollection::save() そして MongoCollection::ensureIndex() はすべて、このオプションをサポートしています)。 デフォルト値 (1) の場合、安全な操作は、 データベースサーバーでの操作が一度成功すれば結果を返します。 セカンダリへのレプリケーションが完了する前にサーバーが落ちてしまうと、 その操作が永遠に失われてしまう可能性があります。そこで w の値を 1 より大きく設定し、 少なくとも一台のセカンダリでの操作が完了してからでないと処理が成功したと見なさないようにするのです。

たとえば w を 2 にすると、メインのサーバー以外に ひとつのセカンダリ上で操作が記録されない限り、ドライバは MongoCursorException をスローします。 w の数をプライマリと全セカンダリの総数に設定したくなるかもしれませんが、 もしそうすると、ひとつのセカンダリがダウンしただけで操作が失敗して例外が発生するようになります。 通常は、w=2 (プライマリ、そしてセカンダリ一台) としておくのがいちばん安全でしょう。

wtimeout
10000

MongoDB::$w のレプリケーションが完了するまでに待つミリ秒数。 このクラスの派生クラスである MongoCollection に継承されます。 w の機能が使えるのは、バージョン 1.5.1 以降の MongoDB サーバーと バージョン 1.0.8 以降のドライバを使っている場合のみです。

wtimeout を設定しなければ、w のサーバーへのレプリケーションが完了するまでずっと待ち続けます。 ドライバのデフォルトは 10 秒ですが、この値を変更して挙動を変えることができます。

参考

MongoDB コアドキュメントの » データベース を参照ください。

目次

add a note add a note

User Contributed Notes 3 notes

up
6
jeromakay at yahoo dot com
13 years ago
based on what I've read and then applied, you don't have to specifically create a database or table, you just initialize it.

Indeed, files are not being written inside /data/db, but they will the first moment you start adding data.

So, I'm taking as an example Twitter, with no db defined, I'm still going to have the db available if I run this code:

<?php

define
('TWITTER_API_VERSION', 1);

date_default_timezone_set("Europe/Dublin");

try
{
   
$m = new Mongo(); // connect
   
$db = $m->selectDB("example");
}
catch (
MongoConnectionException $e )
{
    echo
'<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';
    exit();
}

$updates = file_get_contents( "http://api.twitter.com/". TWITTER_API_VERSION ."/statuses/public_timeline.json" );
$updates = json_decode( $updates );

if (
$updates && is_array( $updates ) && count( $updates ) )
{
    foreach (
$updates as $update )
    {   
       
$db->users->insert( $update );
    }
}

?>

Hope this was helpful!

Good luck!
Vladimir Ghetau
up
-1
careddumattia at gmail dot com
8 years ago
mongo extension is deprecated.

pecl install mongodb
up
-44
m dot espositoii at yahoo dot com
12 years ago
With Mongo it'll automatically create the collection, so just start using it and it'll do the creation itself.

In other words... just use SelectCollection, if it doesn't exist, it will after that so you can drop it.
To Top