(PECL mongo >=0.9.0)
MongoCollection::find — Querys this collection
The fields for which to search.
Fields of the results to return.
Returns a cursor for the search results.
Example #1 MongoCollection::find() example
This example demonstrates how to search for a range.
<?php
// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
?>
See MongoCursor for more information how to work with cursors.
Example #2 MongoCollection::find() example using $where
This example demonstrates how to search a collection using javascript code to reduce the resultset.
<?php
$collection = $db->my_db->articles;
$js = "function() {
return this.type == 'homepage' || this.featured == true;
}";
$articles = $collection->find(array('$where' => $js));
?>
Example #3 MongoCollection::find() example using $in
This example demonstrates how to search a collection using the $in operator.
<?php
$collection = $db->my_db->articles;
$articles = $collection->find(array(
'type' => array('$in' => array('homepage', 'editorial'))
));
?>
Example #4 Getting results as an array
This returns a MongoCursor. Often, when people are starting out, they are more comfortable using an array. To turn a cursor into an array, use the iterator_to_array() function.
<?php
$cursor = $collection->find();
$array = iterator_to_array($cursor);
?>
Using iterator_to_array() forces the driver to load all of the results into memory, so do not do this for result sets that are larger than memory!
Also, certain system collections do not have an _id field. If you are dealing with a collection that might have documents without _ids, pass FALSE as the second argument to iterator_to_array() (so that it will not try to use the non-existent _id values as keys).
MongoDB core docs on » find.