If you are getting "writing more" shown at random places on the screen, it's a MongoDB connector bug in 1.0.5.
Bug report: http://jira.mongodb.org/browse/PHP-91
Update to the latest connector driver and it should go away.
チュートリアル
目次
- 接続の作成
- データベースの取得
- コレクションの取得
- ドキュメントの追加
- MongoCollection::findOne によるドキュメントの検索
- 複数のドキュメントの追加
- コレクション内のドキュメント数
- カーソルを使った全ドキュメントの取得
- 問い合わせの条件の設定
- クエリによる複数ドキュメントの取得
- インデックスの作成
これは、10gen がサポートする MongoDB 用 PHP ドライバです。
簡単なコード例をもとに、MongoDB への接続やドキュメントの追加、 ドキュメントの問い合わせ、問い合わせ結果の反復処理、 そして接続の切断の方法を示します。 各ステップの詳細については後に続くチュートリアルを参照ください。
<?php
// 接続
$m = new MongoClient();
// データベースの選択
$db = $m->comedy;
// コレクション (リレーショナルデータベースのテーブルみたいなもの) の選択
$collection = $db->cartoons;
// レコードの追加
$document = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($document);
// 構造が異なる別のレコードの追加
$document = array( "title" => "XKCD", "online" => true );
$collection->insert($document);
// コレクション内の全件の検索
$cursor = $collection->find();
// 結果の反復処理
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
上の例の出力は以下となります。
Calvin and Hobbes XKCD
Josh Heidenreich ¶
2 years ago
php at whoah dot net ¶
3 years ago
Make sure array keys consecutive before inserting. As of 1.0.6 driver, the following will end up as an object of key:value pairs, instead of an array, because it's trying to maintain the 0 and 2 keys:
$array = array('a', 'b', 'c');
unset($array[1]);
$document = array(
'embedded' => $array,
);
// assuming local
$mongo = new Mongo();
$mongo->test->test->insert($document);
mongodb result:
{ "_id" : ObjectId(...), "embedded" : { "0" : "a", "2" : "c" } }
This is bad if you plan on indexing the embedded property as an array because objects and arrays are indexed differently.
Whether the behaviour will change or not, this is logged here: http://jira.mongodb.org/browse/PHP-104
If you know about it, it's not major, just use a sort() before inserting, or use array_* methods to remove elements instead of unset() -- anything that will re-adjust keys.
