php-doc-en/reference/mongo/mongocollection/find.xml
Kristina Chodorow ba819856bb addendum on collections w/out _ids
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@305515 c90b9560-bf6c-de11-be94-00142212c4b1
2010-11-18 19:10:04 +00:00

168 lines
4.4 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.find" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::find</refname>
<refpurpose>Querys this collection</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>MongoCursor</type><methodname>MongoCollection::find</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>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>query</parameter>
</term>
<listitem>
<para>
The fields for which to search.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>fields</parameter>
</term>
<listitem>
<para>
Fields of the results to return.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a cursor for the search results.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoCollection::find</function> example</title>
<para>This example demonstrates how to search for a range.</para>
<programlisting role="php">
<![CDATA[
<?php
// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
?>
]]>
</programlisting>
<para>
See <classname>MongoCursor</classname> for more information how to work with cursors.
</para>
</example>
<example>
<title><function>MongoCollection::find</function> example using $where</title>
<para>This example demonstrates how to search a collection using javascript code to reduce the resultset.</para>
<programlisting role="php">
<![CDATA[
<?php
$collection = $db->my_db->articles;
$js = "function() {
return this.type == 'homepage' || this.featured == true;
}";
$articles = $collection->find(array('$where' => $js));
?>
]]>
</programlisting>
</example>
<example>
<title><function>MongoCollection::find</function> example using $in</title>
<para>This example demonstrates how to search a collection using the $in operator.</para>
<programlisting role="php">
<![CDATA[
<?php
$collection = $db->my_db->articles;
$articles = $collection->find(array(
'type' => array('$in' => array('homepage', 'editorial'))
));
?>
]]>
</programlisting>
</example>
<example>
<title>Getting results as an array</title>
<para>
This returns a <classname>MongoCursor</classname>. Often, when people are
starting out, they are more comfortable using an array. To turn a cursor
into an array, use the <function>iterator_to_array</function> function.
</para>
<programlisting role="php">
<![CDATA[
<?php
$cursor = $collection->find();
$array = iterator_to_array($cursor);
?>
]]>
</programlisting>
<para>
Using <function>iterator_to_array</function> forces the driver to load all of
the results into memory, so do not do this for result sets that are larger
than memory!
</para>
<para>
Also, certain system collections do not have an <literal>_id</literal>
field. If you are dealing with a collection that might have documents
without <literal>_id</literal>s, pass &false; as the second argument to
<function>iterator_to_array</function> (so that it will not try to use the
non-existent <literal>_id</literal> values as keys).
</para>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
MongoDB core docs on <link xlink:href="&url.mongodb.dochub.find;">find</link>.
</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
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
-->