downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

MongoDB::authenticate> <Mongo::__toString
[edit] Last updated: Fri, 10 Feb 2012

view this page in

MongoDB クラス

(バージョン情報なし。おそらく SVN 版にしか存在しないでしょう)

導入

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

<?php

$m 
= new Mongo(); // 接続
$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 $MongoDB->w = 1 ;
public integer $wtimeout = 10000 ;
/* メソッド */
public array MongoDB::authenticate ( string $username , string $password )
public array MongoDB::command ( array $command [, array $options = array() ] )
public MongoDB::__construct ( Mongo $conn , string $name )
public MongoCollection MongoDB::createCollection ( string $name [, bool $capped = FALSE [, int $size = 0 [, int $max = 0 ]]] )
public array MongoDB::createDBRef ( string $collection , mixed $a )
public array MongoDB::drop ( void )
public array MongoDB::dropCollection ( mixed $coll )
public array MongoDB::execute ( mixed $code [, array $args = array() ] )
public bool MongoDB::forceError ( void )
public MongoCollection MongoDB::__get ( string $name )
public array MongoDB::getDBRef ( array $ref )
public MongoGridFS MongoDB::getGridFS ([ string $prefix = "fs" ] )
public int MongoDB::getProfilingLevel ( void )
public bool MongoDB::getSlaveOkay ( void )
public array MongoDB::lastError ( void )
public array MongoDB::listCollections ( void )
public array MongoDB::prevError ( void )
public array MongoDB::repair ([ bool $preserve_cloned_files = FALSE [, bool $backup_original_files = FALSE ]] )
public array MongoDB::resetError ( void )
public MongoCollection MongoDB::selectCollection ( string $name )
public int MongoDB::setProfilingLevel ( int $level )
public bool MongoDB::setSlaveOkay ([ bool $ok = true ] )
public string MongoDB::__toString ( void )
}

定義済み定数

MongoDB ログレベル

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

フィールド

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 コアドキュメントの » データベース を参照ください。

目次



MongoDB::authenticate> <Mongo::__toString
[edit] Last updated: Fri, 10 Feb 2012
 
add a note add a note User Contributed Notes MongoDB
m dot espositoii at yahoo dot com 27-Aug-2011 03:11
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.
jeromakay at yahoo dot com 20-Jan-2011 12:19
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
jonwage at gmail dot com 01-May-2010 11:56
Just a note that if you use selectDB() and only select it, the database will not be created. In PHPMongoDBAdmin(http://github.com/jwage/php-mongodb-admin) I wanted a way to create a database through a form so I needed to create a dummy collection and drop it in order for the database to be created. MongoDB has a drop() method but no create() method.

<?php
$mongo
->selectDB('db')->createCollection('__tmp_collection_');
$mongo->selectDB('db')->dropCollection('__tmp_collection_');
?>

Any better way to do this?

 
show source | credits | stats | sitemap | contact | advertising | mirror sites