(PECL mongo >=0.9.0)
MongoCollection::findOne — Querys this collection, returning a single element
$query= array() [, array
$fields= array() ]] )
The fields for which to search. MongoDB's query language is quite extensive. The PHP driver will in almost all cases pass the query straight through to the server, so reading the MongoDB core docs on » find is a good idea.Warning
Please make sure that for all special query operaters (starting with $) you use single quotes so that PHP doesn't try to replace "$exists" with the value of the variable $exists.
Fields of the results to return. The array is in the format array('fieldname' => true, 'fieldname2' => true). The _id field is always returned.
Returns record matching the search or
Throws MongoConnectionException if it cannot reach the database.
Example #1 MongoCollection::findOne document by its id.
This example demonstrates how to find a single document in a collection by its id.
$articles = $mongo->my_db->articles;
$article = $articles->findOne(array('_id' => new MongoId('47cc67093475061e3d9536d2')));
Example #2 MongoCollection::findOne document by some condition.
This example demonstrates how to find a single document in a collection by some condition and limiting the returned fields.
$users = $mongo->my_db->users;
$user = $users->findOne(array('username' => 'jwage'), array('password'));
The above example will output something similar to:
Array ( [_id] => MongoId Object ( ) [password] => test )
Notice how even though the document does have a username field, we limited the results to only contain the password field.