phpのMongoクラスでfind()してもレコードが返ってこないときの対処法

ソースコード

<?php
    // 接続
    $m = new Mongo();

    // データベースの選択
    $db = $m->comedy;

    // コレクション (リレーショナルデータベースのテーブルみたいなもの) の選択
    $collection = $db->cartoons;

    // レコードの追加
    $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
    $collection->insert($obj);

    // 構造が異なる別のレコードの追加
    $obj = array( "title" => "XKCD", "online" => true );
    $collection->insert($obj);

    // コレクション内の全件の検索
    $cursor = $collection->find();
    var_dump($cursor);

結果

object(MongoCursor)#45 (0) {
}

対処法

find()によって返されるMongoCursorはあくまでカーソルなので、
foreachで一つずつ取ってこないと中身が取れない。

<?php
    // コレクション内の全件の検索
    $cursor = $collection->find();
    // 結果の反復処理
    foreach ($cursor as $obj) {
        var_dump($obj);
    }

修正後の結果

array(3) {
  ["_id"]=>
  object(MongoId)#46 (1) {
    ["$id"]=>
    string(24) "4ec229556803faad59000000"
  }
  ["title"]=>
  string(17) "Calvin and Hobbes"
  ["author"]=>
  string(14) "Bill Watterson"
}
array(3) {
  ["_id"]=>
  object(MongoId)#42 (1) {
    ["$id"]=>
    string(24) "4ec229556803faad59000001"
  }
  ["title"]=>
  string(4) "XKCD"
  ["online"]=>
  bool(true)
}