(PECL mongo >=0.9.0)
MongoCollection::find — コレクションに問い合わせ、結果セットの MongoCursor を返す
$query = array()
[, array $fields = array()
]] ) : MongoCursor
query
検索したいフィールド。 MongoDB のクエリ言語は極めて幅広いものです。 PHP ドライバはほとんどの場合クエリをそのままサーバーに流すので、 MongoDB コアドキュメントの » find を読むといいでしょう。
クエリの特別な演算子 ($ ではじまるもの) は、すべてシングルクォートで囲まなければならないことに注意しましょう。 "$exists" などとすると、PHP がそれを変数 $exists の値で置き換えてしまいます。
fields
返される結果のフィールド。配列の形式は array('fieldname' => true, 'fieldname2' => true) のようになります。_id フィールドは常に返されます。
検索結果のカーソルを返します。
例1 MongoCollection::find() の例
この例は、基本的な検索オプションを示します。
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'produce');
// フルーツを検索します
$fruitQuery = array('Type' => 'Fruit');
$cursor = $collection->find($fruitQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
// 甘いやつを検索します (Taste は Details の子です)
$sweetQuery = array('Details.Taste' => 'Sweet');
echo "Sweet\n";
$cursor = $collection->find($sweetQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
上の例の出力は以下となります。
array(4) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "50a87dd084f045a19b220dd6"
}
["Name"]=>
string(5) "Apple"
["Type"]=>
string(5) "Fruit"
["Details"]=>
array(2) {
["Taste"]=>
string(5) "Sweet"
["Colour"]=>
string(3) "Red"
}
}
array(4) {
["_id"]=>
object(MongoId)#8 (1) {
["$id"]=>
string(24) "50a87de084f045a19b220dd7"
}
["Name"]=>
string(5) "Lemon"
["Type"]=>
string(5) "Fruit"
["Details"]=>
array(2) {
["Taste"]=>
string(4) "Sour"
["Colour"]=>
string(5) "Green"
}
}
Sweet:
array(4) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "50a87dd084f045a19b220dd6"
}
["Name"]=>
string(5) "Apple"
["Type"]=>
string(5) "Fruit"
["Details"]=>
array(2) {
["Taste"]=>
string(5) "Sweet"
["Colour"]=>
string(3) "Red"
}
}
カーソルの使いかたについての詳しい情報は MongoCursor を参照ください。
例2 MongoCollection::find() の例
この例は、範囲指定検索の方法を示します。
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
// 5 < x < 20 であるドキュメントを検索します
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
上の例の出力は以下となります。
array(2) {
["_id"]=>
object(MongoId)#10 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000000"
}
["x"]=>
int(12)
}
array(2) {
["_id"]=>
object(MongoId)#11 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000001"
}
["x"]=>
int(12)
}
カーソルの挙動についての詳細な情報は MongoCursor を参照ください。
例3 MongoCollection::find() での $where の使用例
この例は、コレクションの検索に javascript コードを使って結果セットを小さくする方法を示します。
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$js = "function() {
return this.name == 'Joe' || this.age == 50;
}";
$cursor = $collection->find(array('$where' => $js));
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
上の例の出力は以下となります。
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
例4 MongoCollection::find() での $in の使用例
この例は、コレクションの検索に $in 演算子を使う方法を示します。
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$cursor = $collection->find(array(
'name' => array('$in' => array('Joe', 'Wendy'))
));
?>
上の例の出力は以下となります。
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
例5 配列形式での結果の取得
このメソッドは MongoCursor を返します。 しかし、配列形式のほうが使いやすいという人もいるでしょう。 カーソルを配列に変換するには iterator_to_array() 関数を使います。
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$cursor = $collection->find();
$array = iterator_to_array($cursor);
?>
上の例の出力は以下となります。
array(3) {
["4ebc40af10b89f5149000000"]=>
array(2) {
["_id"]=>
object(MongoId)#6 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000000"
}
["x"]=>
int(12)
}
["4ebc40af10b89f5149000001"]=>
array(2) {
["_id"]=>
object(MongoId)#11 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000001"
}
["x"]=>
int(12)
}
["4ebc40af10b89f5149000002"]=>
array(3) {
["_id"]=>
object(MongoId)#12 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
}
iterator_to_array() を使うと、結果全体をメモリに展開することになります。 メモリサイズを超える結果セットではこれを使わないでください!
また、一部のシステムコレクションには _id
フィールドを持たないものがあります。_id
がないドキュメントを含むかもしれないコレクションを扱う場合は、
iterator_to_array() の二番目の引数に FALSE
を渡します (そうすれば、存在しない
_id の値をキーとして使うことがなくなります)。