For whatever reason the @ symbol throws the php implementation of findOne for a loop.
I think this is the same problem that ejs5 was experiencing but if you are trying to do a findOne on an email address like
$an_array = $col->findOne('email' => 'fred.trotter@example.com');
You will get nothing.
But if you do:
$an_cursor = $col->find('email' => 'fred.trotter@example.com');
$an_array = $an_cursor->getNext();
you will have the same result.
Interestingly I was not able to replicate this problem with the '&' symbol that ejs5 complained about. Perhaps this is a bug being chased down across versions. Hope this helps someone.
-FT
MongoCollection::findOne
(PECL mongo >=0.9.0)
MongoCollection::findOne — Querys this collection, returning a single element
Descrierea
$query = array()
[, array $fields = array()
]] )As opposed to MongoCollection::find(), this method will return only the first result from the result set, and not a MongoCursor that can be iterated over.
Parametri
-
query -
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.
AvertizarePlease 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 -
Fields of the results to return. The array is in the format array('fieldname' => true, 'fieldname2' => true). The _id field is always returned.
Valorile întoarse
Returns record matching the search or NULL.
Erori/Excepții
Throws MongoConnectionException if it cannot reach the database.
Exemple
Example #1 MongoCollection::findOne() document by its id.
This example demonstrates how to find a single document in a collection by its id.
<?php
$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.
<?php
$users = $mongo->my_db->users;
$user = $users->findOne(array('username' => 'jwage'), array('password'));
print_r($user);
?>
Exemplul de mai sus va afișa ceva similar cu:
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.
Vedeți de asemenea
- MongoCollection::find() - Querys this collection, returning a MongoCursor for the result set
- MongoCollection::insert() - Inserts an array into the collection
- MongoDB core docs on » find.
Special characters seem to be automatically escaped by the Mongo driver.
<?php
$db = $mongo->my_db->wireless_service_providers;
$provider = $db->findOne(array("name" => "AT&T"), array('_id' => 1));
print_r($provider);
?>
if the value is stored as "AT&T" in the document you will get
Array([_id]=>)
but if the value is stored as "AT&T" it will return
Array ( [_id] => MongoId Object ( ) )
There is also a notation to retrieve all fields, but the specified ones
<?php
$users = $mongo->my_db->users;
$user = $users->findOne(array('username' => 'jwage'), array('password' => 0));
print_r($user);
?>
Will return all fields of the user, but the password field.
