これは、mongodbのコレクションからデータをフェッチするための私のPHPコードです。
$m = new Mongo('localhost');
$m->connect();
$db = $m->mydb;
$collection = $db->fields_current;
$cursor = $collection->find();
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
}
コードはコレクションに対して正常に機能しますfields_current
。しかし、私はこのようなコレクションを持っています。fields_current.node
ここでnode
、はコレクションの名前付きグループです。名前付きグループからデータをフェッチするにはどうすればよいですか。
編集:
PeterMの答えに従った後、私は空のMongoCursorオブジェクトを取得しています。これは私の新しいコードです:
$m = new Mongo('localhost');
$m->connect();
$db = $m->mydb;
$collection = $db->fields_current;
$query = array(array('group_name' => 'node', 'type' => 'content-type'), array('title' => 1)); // SELECT title FROM fields_current WHERE group_name = 'node' AND type = 'content-type'
$cursor = $collection->find($query);
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
}
詳細:私はDrupalを使用node
しており、「ノード」テーブルを参照していますtype
。これは「ノードタイプ」です。コレクション名はfields_current.node
であり、レコード構造は次のとおりです。
array (
'_id' => new MongoInt32(37),
'_type' => 'node',
'_bundle' => 'content-type',
'_revision_id' => new MongoInt32(37),
'nid' => new MongoInt32(37),
'vid' => new MongoInt32(37),
'type' => 'content-type',
'language' => 'und',
'title' => 'title',
'uid' => new MongoInt32(1),
'status' => new MongoInt32(1),
'created' => new MongoInt32(1342065549),
'changed' => new MongoInt32(1342065549),
'comment' => new MongoInt32(1),
'promote' => new MongoInt32(0),
'sticky' => new MongoInt32(0),
'tnid' => new MongoInt32(0),
'translate' => new MongoInt32(0),
'field_cutom_field' =>
array (
'value' => 'foobar',
),
)