MongoCollection
PHP Manual

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.

Ошибки

Throws MongoConnectionException if it cannot reach the database.

Примеры

Пример #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')));

?>

Пример #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);

?>

Результатом выполнения данного примера будет что-то подобное:

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.


MongoCollection
PHP Manual