Commit bb128ed0 authored by Hannes Magnusson's avatar Hannes Magnusson

PHP-1335: Add Collection::findOne() helper

parent 0509f3c2
...@@ -78,6 +78,31 @@ class Collection { ...@@ -78,6 +78,31 @@ class Collection {
return $cursor; return $cursor;
} }
/**
* Performs a find (query) on the collection, returning at most one result
*
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
* @see MongoDB\Collection::getFindOptions() for supported $options
*
* @param array $filter The find query to execute
* @param array $options Additional options
* @return array|false The matched document, or false on failure
*/
function findOne(array $filter = array(), array $options = array()) {
$options = array_merge($this->getFindOptions(), array("limit" => 1), $options);
$query = $this->_buildQuery($filter, $options);
$cursor = $this->manager->executeQuery($this->ns, $query, $this->rp);
$array = iterator_to_array($cursor);
if ($array) {
return $array[0];
}
return false;
}
/** /**
* Retrieves all find options with their default values. * Retrieves all find options with their default values.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment