downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

MongoCollection::getDBRef> <MongoCollection::find
[edit] Last updated: Fri, 18 Sep 2009

view this page in

MongoCollection::findOne

(PECL mongo >=0.9.0)

MongoCollection::findOneQuerys this collection, returning a single element

Описание

public array MongoCollection::findOne ([ array $query = array() [, array $fields = array() ]] )

Параметри

query

The fields for which to search.

fields

Fields of the results to return.

Връщани стойности

Returns record matching the search or NULL.



MongoCollection::getDBRef> <MongoCollection::find
[edit] Last updated: Fri, 18 Sep 2009
 
add a note add a note User Contributed Notes MongoCollection::findOne
fred dot trotter at gmail dot com 14-Apr-2012 05:35
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
ejs5 at g2link dot com 09-Dec-2010 08:30
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&AMP;T" it will return

Array ( [_id] => MongoId Object ( ) )
dominik at dokdok dot com 30-Sep-2010 11:44
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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites