An undocumented feature is that you can pass a stdClass instead of an array to $fields which will allow you to get around the issue of PHP casting string values to integers which mongo doesn't like. i.e.
array('123' => 0, '321' => 0);
becomes:
array(213 => 0, 312 => 0);
which will trip up with the error:
PHP Fatal error: Uncaught exception 'MongoException' with message 'field names must be strings'
The fix is as simple as:
<?php
$fields = new stdClass;
$fields->{123} = 0;
$fields->{321} = 0;
?>
MongoCollection::find
(PECL mongo >=0.9.0)
MongoCollection::find — コレクションに問い合わせ、結果セットの MongoCursor を返す
説明
パラメータ
-
query -
検索したいフィールド。 MongoDB のクエリ言語は極めて幅広いものです。 PHP ドライバはほとんどの場合クエリをそのままサーバーに流すので、 MongoDB コアドキュメントの » find を読むといいでしょう。
警告クエリの特別な演算子 ($ ではじまるもの) は、すべてシングルクォートで囲まなければならないことに注意しましょう。 "$exists" などとすると、PHP がそれを変数 $exists の値で置き換えてしまいます。
-
fields -
返される結果のフィールド。配列の形式は array('fieldname' => true, 'fieldname2' => true) のようになります。_id フィールドは常に返されます。
返り値
検索結果のカーソルを返します。
例
例1 MongoCollection::find() の例
この例は、範囲指定検索の方法を示します。
<?php
$m = new Mongo();
$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 を参照ください。
例2 MongoCollection::find() での $where の使用例
この例は、コレクションの検索に javascript コードを使って結果セットを小さくする方法を示します。
<?php
$m = new Mongo();
$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)
}
例3 MongoCollection::find() での $in の使用例
この例は、コレクションの検索に $in 演算子を使う方法を示します。
<?php
$m = new Mongo();
$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)
}
例4 配列形式での結果の取得
このメソッドは MongoCursor を返します。 しかし、配列形式のほうが使いやすいという人もいるでしょう。 カーソルを配列に変換するには iterator_to_array() 関数を使います。
<?php
$m = new Mongo();
$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 の値をキーとして使うことがなくなります)。
参考
- MongoCollection::findOne() - コレクションに問い合わせ、単一の要素を返す
- MongoCollection::insert() - 配列をコレクションに追加する
- MongoDB コアドキュメントの » find
This will work with versions >=1.5.3, please note that this is just a example of the way to use the or statement.
<?php
$connection = new Mongo();
$db = $connection->test;
$collection = $db->test;
// Clean the DB before the test.
$collection->drop();
$collection = $db->test;
$apple = array(
'fruit' => 'Apple',
'type' => 'Juice',
);
$orange = array(
'fruit' => 'Orange',
'type' => 'Marmalade',
);
$collection->insert($apple);
$collection->insert($orange);
// Basic find
$results = $collection->find(array('fruit' => 'Apple'));
foreach($results as $result)
{
echo sprintf("Fruit: %s, Type: %s%s", $result['fruit'], $result['type'], PHP_EOL);
}
?>
Output:
Fruit: Apple, Type: Juice
Now an advanced search with "or" statement.
<?php
// Advanced find with "OR" note the double array.
// if you use double quotes escape the or "\$or"
$results = $collection->find( array( '$or' => array( array('fruit' => 'Apple'), array('fruit' => 'Orange') ) ) );
foreach($results as $result)
{
echo sprintf("Fruit: %s, Type: %s%s", $result['fruit'], $result['type'], PHP_EOL);
}
?>
Output:
Fruit: Apple, Type: Juice
Fruit: Orange, Type: Marmalade
