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

search for in the

MongoCollection::__get> <MongoCollection::find
[edit] Last updated: Fri, 25 May 2012

view this page in

MongoCollection::findOne

(PECL mongo >=0.9.0)

MongoCollection::findOneコレクションに問い合わせ、単一の要素を返す

説明

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

MongoCollection::find() とは対照的に、このメソッドは結果セットの 最初の結果だけを返します。 MongoCursor を返すのではないので、その後の反復処理はできません。

パラメータ

query

検索したいフィールド。 MongoDB のクエリ言語は極めて幅広いものです。 PHP ドライバはほとんどの場合クエリをそのままサーバーに流すので、 MongoDB コアドキュメントの » find を読むといいでしょう。

警告

クエリの特別な演算子 ($ ではじまるもの) は、すべてシングルクォートで囲まなければならないことに注意しましょう。 "$exists" などとすると、PHP がそれを変数 $exists の値で置き換えてしまいます。

fields

返される結果のフィールド。配列の形式は array('fieldname' => true, 'fieldname2' => true) のようになります。_id フィールドは常に返されます。

返り値

検索にマッチしたレコード、あるいは NULL を返します。

エラー / 例外

データベースに接続できない場合に MongoConnectionException をスローします。

例1 MongoCollection::findOne() での id による検索

この例は、コレクション内のドキュメントを id から検索する方法を示します。

<?php

$articles 
$mongo->my_db->articles;

$article $articles->findOne(array('_id' => new MongoId('47cc67093475061e3d9536d2')));

?>

例2 MongoCollection::findOne() での複数条件の使用

この例は、コレクション内のドキュメントを複数の条件で検索し、返されるフィールドを絞り込む方法を示します。

<?php

$users 
$mongo->my_db->users;
$user $users->findOne(array('username' => 'jwage'), array('password'));
print_r($user);

?>

上の例の出力は、 たとえば以下のようになります。

Array
(
    [_id] => MongoId Object
        (
        )

    [password] => test
)

たとえドキュメントに username フィールドがあっても、 返される結果には password フィールドしか含まれないことに注目しましょう。

参考



MongoCollection::__get> <MongoCollection::find
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes MongoCollection::findOne
fred dot trotter at gmail dot com 14-Apr-2012 02: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 10-Dec-2010 05: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 08: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