©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PECL mongo >=0.9.0)
MongoCollection::find — 查询该集合,并返回结果集的 MongoCursor
$query
= array()
[, array $fields
= array()
]] )
query
要搜索的字段。 MongoDB 的查询语言十分宽泛。 PHP 驱动在几乎所有的情况下会把查询直接传入服务器,所以阅读 MongoDB 关于 » find 的核心文档是个不错的主意。
请确保所有指定的查询操作符(以 $ 开头)是用单引号的,这样 PHP 才不会尝试用 $exists 变量的值来替换 "$exists" 命令。
fields
返回结果的字段。Array 的格式是 array('fieldname' => true, 'fieldname2' => true)。 _id 字段总会返回。
返回搜索结果的游标。
Example #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 is a child of 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。
Example #2 MongoCollection::find() 例子
这个例子演示了如何搜索一个范围。
<?php
$m = new MongoClient ();
$db = $m -> selectDB ( 'test' );
$collection = new MongoCollection ( $db , 'phpmanual' );
// search for documents where 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。
Example #3 使用 $where 的 MongoCollection::find() 例子
这个例子演示了如何搜索一个集合,并用 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) }
Example #4 使用 $in 的 MongoCollection::find() 例子
这个例子演示了使用 $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) }
Example #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 字段的集合,需要将 FALSE
传入
iterator_to_array() 第二个参数(这样它不会尝试使用不存在的 _id 的值作为数组键)。
[#1] bronius dot motekaitis at gmail dot com [2015-03-02 20:30:12]
As the docs specify, '$or' conditions (and similar) get passed right on to MongoDB directly. It appears that to make a simple "field, $or, field" compound query work, all parts must be wrapped as a gigantic $and.
Here's how I got a find(), findOne(), and findAndModify() to obey such a compound $or for matching on fields, one of which is represented in data as either a string or integer:
<?php
$query =
array('$and' =>
array(
array('assessment_id' => $doc->assessment_id),
array('$or' =>
array(
array('participant_id' => $doc->participant_id),
array('participant_id' => (string)$doc->participant_id),
),
),
array('measure_id' => $doc->measure_id)
),
);
$thedoc = $collection->findOne($query);
return $thedoc;
?>
[#2] Nanhe Kumar [2013-11-07 21:13:29]
<?php
$m = new MongoClient();
$db = $m->selectDB('school');
$collection = new MongoCollection($db, 'student');
//Find where class=5
$where=array('class'=>5);
$cursor = $collection->find($where);
//Find where class !=5
$where=array('class' => array('$ne'=>5));
$cursor = $collection->find($where);
//Find where age >20
$where=array('age' => array('$gt'=>20));
$cursor = $collection->find($where);
//Find where age >=20
$where=array('age' => array('$gte'=>20));
$cursor = $collection->find($where);
//Find where age <20
$where=array('age' => array('$le'=>20));
$cursor = $collection->find($where);
//Find where age <=20
$where=array('age' => array('$lte'=>20));
$cursor = $collection->find($where);
//Finc where class=10 or marks=80
$where=array( '$or' => array( array(' class' =>10), array('marks'=>80) ) );
$cursor = $collection->find($where);
//Finc where class=12 AND marks=70
$where=array( '$and' => array( array(' class' =>12), array('marks'=>70) ) );
$cursor = $collection->find($where);
?>
[#3] artusdebenque at yahoo dot fr [2013-10-28 13:17:03]
For the fields parameter, the documentaion says: "The _id field is always returned".
Knowing that mongodb allows you to uncheck the _id field ("the _id field is the only field that you can explicitly exclude"; source: http://docs.mongodb.org/manual/reference/method/db.collection.find/#db.collection.find), I tried it with php and it works : you can exclude the _id field.
Example : the following fields parameter will exclude the field "_id"
$fields = array('timestamp' => true, 'rank' => true, '_id' => false);
[#4] nospam at alexyves dot fr [2010-12-09 06:28:39]
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