PHP Doku:: Querys this collection, returning a single element - mongocollection.findone.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenMongoDB Native DriverCore ClassesThe MongoCollection classMongoCollection::findOne

Ein Service von Reinhard Neidl - Webprogrammierung.

The MongoCollection class

<<MongoCollection::find

MongoCollection::__get>>

MongoCollection::findOne

(PECL mongo >=0.9.0)

MongoCollection::findOneQuerys this collection, returning a single element

Beschreibung

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

Parameter-Liste

query

The fields for which to search.

fields

Fields of the results to return.

Rückgabewerte

Returns record matching the search or NULL.

Fehler/Exceptions

Throws MongoConnectionException if it cannot reach the database.

Beispiele

Beispiel #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')));

?>

Beispiel #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);

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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.


2 BenutzerBeiträge:
- Beiträge aktualisieren...
ejs5 at g2link dot com
10.12.2010 5: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.09.2010 20: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)