php-doc-en/reference/mongo/functions/mongo-find-one.xml
2009-02-25 22:50:52 +00:00

165 lines
3.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.3 $ -->
<refentry xml:id="function.mongo-find-one" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>mongo_find_one</refname>
<refpurpose>Queries the database for a single record</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>mongo_find_one</methodname>
<methodparam><type>resource</type><parameter>connection</parameter></methodparam>
<methodparam><type>string</type><parameter>ns</parameter></methodparam>
<methodparam><type>array</type><parameter>query</parameter></methodparam>
</methodsynopsis>
<para>
Queries the database for a single record. This can also be used for database commands.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>connection</parameter></term>
<listitem>
<para>
The database connection to use.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>ns</parameter></term>
<listitem>
<para>
A string of the form [database_name].[collection_name].
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>query</parameter></term>
<listitem>
<para>
An array representing the desired results.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns an array if an element matching the query was found, null otherwise.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>mongo_find_one</function> example</title>
<para>
This example shows how to find the most recently inserted record
in the collection "bar" in the database "foo".
</para>
<programlisting role="php">
<![CDATA[
<?php
$conn = mongo_connect("localhost", true);
if (!$conn) {
die("Could not connect.");
}
mongo_insert($conn, "foo.bar", array( "x" => "y" ));
var_dump(mongo_find_one($conn, "foo.bar", array()));
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(2) {
["_id"]=>
object(MongoId)#1 (1) {
["id"]=>
string(24) "49a2e6f55b8b4a17e843cf3d"
}
["x"]=>
string(1) "y"
}
]]>
</screen>
</example>
<example>
<title><function>mongo_find_one</function> example</title>
<para>
This example shows how to execute a database command using
the "foo" database.
</para>
<programlisting role="php">
<![CDATA[
<?php
$conn = mongo_connect("localhost", true);
if (!$conn) {
die("Could not connect.");
}
var_dump(mongo_find_one($conn, 'foo.$cmd', array('assertinfo' => 1)));
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(7) {
["dbasserted"]=>
bool(false)
["asserted"]=>
bool(false)
["assert"]=>
string(0) ""
["assertw"]=>
string(0) ""
["assertmsg"]=>
string(0) ""
["assertuser"]=>
string(0) ""
["ok"]=>
float(1)
}
]]>
</screen>
</example>
</para>
</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
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../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
-->