php-doc-en/reference/mongo/mongocursor.xml
Jakub Vrana 5e240161c1 Link class members
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@322123 c90b9560-bf6c-de11-be94-00142212c4b1
2012-01-12 06:15:07 +00:00

204 lines
6.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<phpdoc:classref xml:id="class.mongocursor" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The MongoCursor class</title>
<titleabbrev>MongoCursor</titleabbrev>
<partintro>
<!-- {{{ Mongocursor intro -->
<section xml:id="mongocursor.intro">
&reftitle.intro;
<para>
A cursor is used to iterate through the results of a database query. For
example, to query the database and see all results, you could do:
<programlisting role="php">
<![CDATA[
<?php
$cursor = $collection->find();
var_dump(iterator_to_array($cursor));
?>
]]>
</programlisting>
</para>
<para>
You don't generally create cursors using the
<classname>MongoCursor</classname> constructor, you get a new cursor by
calling <function>MongoCollection::find</function> (as shown above).
</para>
<para>
Suppose that, in the example above, <literal>$collection</literal> was a
50GB collection. We certainly wouldn't want to load that into memory all
at once, which is what a cursor is for: allowing the client to access the
collection in dribs and drabs.
</para>
<para>
If we have a large result set, we can iterate through it, loading a few
megabytes of results into memory at a time. For example, we could do:
<programlisting role="php">
<![CDATA[
<?php
$cursor = $collection->find();
foreach ($cursor as $doc) {
// do something to each document
}
?>
]]>
</programlisting>
This will go through each document in the collection, loading and garbage
collecting documents as needed.
</para>
<para>
Note that this means that a cursor does not "contain" the database results,
it just manages them. Thus, if you print a cursor (with, say,
<function>var_dump</function> or <function>print_r</function>), you'll just
get the cursor object, not your documents. To get the documents themselves,
you can use one of the methods shown above.
</para>
</section>
<section>
<title>Cursor Stages</title>
<para>
A <classname>MongoCursor</classname> has two "life stages": pre- and post-
query. When a cursor is created, it has not yet contacted the database, so
it is in its pre-query state. In this state, the client can further specify
what they want the query to do, including adding limits, skips, sorts, and
more advanced options.
</para>
<para>
When the client attempts to get a result (by calling
<function>MongoCursor::next</function>, directly or indirectly), the cursor
moves into the post-query stage. At this point, the query has been executed
by the database and cannot be modified anymore.
</para>
<para>
<programlisting role="php">
<![CDATA[
<?php
$cursor = $collection->find()->limit(10);
// database has not yet been queried, so more search options can be added
$cursor = $cursor->sort(array("a" => 1));
var_dump($cursor->getNext());
// now database has been queried and more options cannot be added
// so this will throw an exception:
$cursor->skip(4);
?>
]]>
</programlisting>
</para>
</section>
<!-- }}} -->
<section xml:id="mongocursor.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>MongoCursor</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>MongoCursor</classname>
</ooclass>
<oointerface>
<interfacename>Iterator</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">Static Fields</classsynopsisinfo>
<fieldsynopsis>
<modifier>static</modifier>
<type>boolean</type>
<varname linkend="mongocursor.props.slaveokay">slaveOkay</varname>
<initializer>&false;</initializer>
</fieldsynopsis>
<fieldsynopsis>
<modifier>static</modifier>
<type>integer</type>
<varname linkend="mongocursor.props.timeout">timeout</varname>
<initializer>20000</initializer>
</fieldsynopsis>
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.mongocursor')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[1])" />
</classsynopsis>
<!-- }}} -->
</section>
<section>
<title>Static Variables</title>
<variablelist>
<varlistentry xml:id="mongocursor.props.slaveokay">
<term><varname>slaveOkay</varname></term>
<listitem>
<para>
If the query should have the "slaveOkay" flag set, which allows reads on
the slave (slaves are, by default, just for backup and unreadable). Can
be overridden with <function>MongoCursor::slaveOkay</function>.
</para>
</listitem>
</varlistentry>
<varlistentry xml:id="mongocursor.props.timeout">
<term><varname>timeout</varname></term>
<listitem>
<para>
Set timeout in milliseconds for all database responses. To wait forever,
use -1. Can be overridden with <function>MongoCursor::timeout</function>.
This does not cause the MongoDB server to cancel the operation, it just
causes the driver to stop waiting for a response and throw a
MongoCursorTimeoutException.
</para>
</listitem>
</varlistentry>
</variablelist>
</section>
<section>
&reftitle.seealso;
<para>
MongoDB core docs on <link xlink:href="&url.mongodb.dochub.cursors;">cursors</link>.
</para>
</section>
</partintro>
&reference.mongo.entities.mongocursor;
</phpdoc:classref>
<!-- 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:"~/.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
-->