|
MongoCollection::findコレクションに問い合わせ、結果セットの MongoCursor を返す 説明
public MongoCursor MongoCollection::find
([ array
$query = array()
[, array $fields = array()
]] )パラメータ
返り値検索結果のカーソルを返します。 例例1 MongoCollection::find の例 この例は、基本的な検索オプションを示します。
<?php上の例の出力は以下となります。
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上の例の出力は以下となります。
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上の例の出力は以下となります。
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
例4 MongoCollection::find での $in の使用例 この例は、コレクションの検索に $in 演算子を使う方法を示します。
<?php上の例の出力は以下となります。
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
例5 配列形式での結果の取得 このメソッドは MongoCursor を返します。 しかし、配列形式のほうが使いやすいという人もいるでしょう。 カーソルを配列に変換するには iterator_to_array 関数を使います。
<?php上の例の出力は以下となります。
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 の二番目の引数に 参考
|