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.
MongoDB クラス
(バージョン情報なし。おそらく SVN 版にしか存在しないでしょう)
導入
このクラスのインスタンスを使用してデータベースとのやりとりを行います。 データベースを取得するには、このようにします。
<?php
$m = new Mongo(); // 接続
$db = $m->selectDB("example");
?>
あまり一般的ではありませんが、 "null"、"[x,y]"、"3"、"\""、"/" などは正しい形式のデータベース名です。
コレクション名とは異なり、データベース名には "$" を含めてもかまいません。
クラス概要
$name
[, bool $capped = FALSE
[, int $size = 0
[, int $max = 0
]]] )$preserve_cloned_files = FALSE
[, bool $backup_original_files = FALSE
]] )定義済み定数
MongoDB ログレベル
-
MongoDB::PROFILING_OFF0 - プロファイリングをオフにします。
-
MongoDB::PROFILING_SLOW1 - 襲い接続 (>100 ms) に対するプロファイリングをオンにします。
-
MongoDB::PROFILING_ON2 - すべての操作に対するプロファイリングをオンにします。
フィールド
-
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 — このデータベースにログインする
- MongoDB::command — データベースコマンドを実行する
- MongoDB::__construct — 新しいデータベースを作成する
- MongoDB::createCollection — コレクションを作成する
- MongoDB::createDBRef — データベース参照を作成する
- MongoDB::drop — このデータベースを削除する
- MongoDB::dropCollection — コレクションを削除する [非推奨]
- MongoDB::execute — JavaScript コードをデータベースサーバー上で実行する
- MongoDB::forceError — データベースのエラーを作成する
- MongoDB::__get — コレクションを取得する
- MongoDB::getDBRef — データベース参照が指すドキュメントを取得する
- MongoDB::getGridFS — このデータベースに格納されているファイルを扱うためのツールキットを取得する
- MongoDB::getProfilingLevel — このデータベースのプロファイリングレベルを取得する
- MongoDB::getSlaveOkay — このデータベースの slaveOkay 設定を取得する
- MongoDB::lastError — 直近のデータベース操作でエラーが発生したかどうかを調べる
- MongoDB::listCollections — このデータベース内のコレクション一覧を取得する
- MongoDB::prevError — データベース操作中の直近に発生したエラーを調べる
- MongoDB::repair — このデータベースを修復・圧縮する
- MongoDB::resetError — データベースで発生したすべてのエラーをクリアする
- MongoDB::selectCollection — コレクションを取得する
- MongoDB::setProfilingLevel — このデータベースのプロファイリングレベルを設定する
- MongoDB::setSlaveOkay — このデータベースの slaveOkay の設定を変更する
- MongoDB::__toString — このデータベースの名前
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
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?
