php-doc-en/reference/mongo/mongocollection/findone.xml
Jeremy Mikola 4fc4e25cbd Document MongoDB maxTimeMS command/query option
Also expanded upon aggregate command options.

https://jira.mongodb.org/browse/PHP-1023


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@333265 c90b9560-bf6c-de11-be94-00142212c4b1
2014-04-04 22:19:19 +00:00

196 lines
5.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<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>
<methodparam choice="opt"><type>array</type><parameter>options</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>
<varlistentry>
<term>
<parameter>options</parameter>
</term>
<listitem>
<para>
This parameter is an associative array of the form
<literal>array("name" => &lt;value&gt;, ...)</literal>. Currently
supported options are:
</para>
<itemizedlist>
&mongo.command.parameters.maxtimems;
</itemizedlist>
</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="changelog"><!-- {{{ -->
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>1.5.0</entry>
<entry>
Added optional <parameter>options</parameter> argument.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</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
-->