1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 332331 $ -->
<refentry xml:id="mongocollection.findone" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::findOne</refname>
<refpurpose>Queries this collection, returning a single element</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>array</type><methodname>MongoCollection::findOne</methodname>
<methodparam choice="opt"><type>array</type><parameter>query</parameter><initializer>array()</initializer></methodparam>
<methodparam choice="opt"><type>array</type><parameter>fields</parameter><initializer>array()</initializer></methodparam>
</methodsynopsis>
<para>
As opposed to <function>MongoCollection::find</function>, this method
will return only the <emphasis>first</emphasis> result from the result set,
and not a <classname>MongoCursor</classname> that can be iterated over.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>query</parameter>
</term>
<listitem>
<para>
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
<link xlink:href="&url.mongodb.dochub.find;">find</link> is a good idea.
</para>
<warning>
<para>
Please make sure that for all special query operaters (starting with
<literal>$</literal>) you use single quotes so that PHP doesn't try to
replace <literal>"$exists"</literal> with the value of the variable
<literal>$exists</literal>.
</para>
</warning>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>fields</parameter>
</term>
<listitem>
<para>
Fields of the results to return. The array is in the format
<literal>array('fieldname' => true, 'fieldname2' => true)</literal>.
The <literal>_id</literal> field is always returned.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns record matching the search or &null;.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws <classname>MongoConnectionException</classname> if it cannot reach the
database.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><methodname>MongoCollection::findOne</methodname> document by its id.</title>
<para>This example demonstrates how to find a single document in a collection by its id.</para>
<programlisting role="php">
<![CDATA[
<?php
$articles = $mongo->my_db->articles;
$article = $articles->findOne(array('_id' => new MongoId('47cc67093475061e3d9536d2')));
?>
]]>
</programlisting>
</example>
<example>
<title><methodname>MongoCollection::findOne</methodname> document by some condition.</title>
<para>This example demonstrates how to find a single document in a collection by some condition and limiting the returned fields.</para>
<programlisting role="php">
<![CDATA[
<?php
$users = $mongo->my_db->users;
$user = $users->findOne(array('username' => 'jwage'), array('password'));
print_r($user);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Array
(
[_id] => MongoId Object
(
)
[password] => test
)
]]>
</screen>
<para>
Notice how even though the document does have a username field, we limited the results
to only contain the password field.
</para>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>MongoCollection::find</function></member>
<member><function>MongoCollection::insert</function></member>
<member>MongoDB core docs on <link xlink:href="&url.mongodb.dochub.find;">find</link>.</member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|