(PECL mongo >=0.9.0)
MongoCollection::find — 锟斤拷询锟矫硷拷锟较o拷锟斤拷锟斤拷锟截斤拷锟斤拷锟斤拷锟� MongoCursor
$query
= array()
[, array $fields
= array()
]] ) : MongoCursor
query
要锟斤拷锟斤拷锟斤拷锟街段★拷 MongoDB 锟侥诧拷询锟斤拷锟斤拷十锟街匡拷锟斤拷 PHP 锟斤拷锟斤拷锟节硷拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟斤拷禄锟窖诧拷询直锟接达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷亩锟� MongoDB 锟斤拷锟斤拷 » find 锟侥猴拷锟斤拷锟侥碉拷锟角革拷锟斤拷锟斤拷锟斤拷锟斤拷狻�
锟斤拷确锟斤拷锟斤拷锟斤拷指锟斤拷锟侥诧拷询锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 $ 锟斤拷头锟斤拷锟斤拷锟矫碉拷锟斤拷锟脚的o拷锟斤拷锟斤拷 PHP 锟脚诧拷锟结尝锟斤拷锟斤拷 $exists 锟斤拷锟斤拷锟斤拷值锟斤拷锟芥换 "$exists" 锟斤拷锟筋。
fields
锟斤拷锟截斤拷锟斤拷锟斤拷侄巍锟紸rray 锟侥革拷式锟斤拷 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() 锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷锟绞撅拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟较o拷锟斤拷锟斤拷 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 锟街段的硷拷锟较o拷锟斤拷要锟斤拷 FALSE
锟斤拷锟斤拷
iterator_to_array() 锟节讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟结尝锟斤拷使锟矫诧拷锟斤拷锟节碉拷 _id 锟斤拷值锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷锟�