php-doc-en/reference/mongo/mongocursor.xml
Jeremy Mikola 9f0e1e36a3 Documentation for MongoCursorInterface
This interface extends Iterator and is implemented internally by MongoCursor and MongoCommandCursor. In addition to the interface methods, "see also" links were added on the implemented methods to point back to the interface documentation (as we did for Iterator methods).

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


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@335760 c90b9560-bf6c-de11-be94-00142212c4b1
2015-01-15 21:10:35 +00:00

221 lines
6.8 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:
<example xml:id="mongocursor.intro-example">
<title><classname>MongoCursor</classname> basic usage</title>
<programlisting role="php">
<![CDATA[
<?php
$cursor = $collection->find();
var_dump(iterator_to_array($cursor));
?>
]]>
</programlisting>
</example>
</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:
<example>
<title>Iterating over <classname>MongoCursor</classname></title>
<programlisting role="php">
<![CDATA[
<?php
$cursor = $collection->find();
foreach ($cursor as $doc) {
// do something to each document
}
?>
]]>
</programlisting>
</example>
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>
<example xml:id="mongocursor.stages.adding-options">
<title>Adding options to <classname>MongoCursor</classname></title>
<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>
</example>
</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>MongoCursorInterface</interfacename>
</oointerface>
<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>30000</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 secondary (secondaries are, by default, just for backup and not
queried). Can be overridden with <function>MongoCursor::slaveOkay</function>.
</para>
<para>
This functionality is <emphasis>deprecated</emphasis>. Please use
<xref linkend="mongo.readpreferences" /> instead.
</para>
</listitem>
</varlistentry>
<varlistentry xml:id="mongocursor.props.timeout">
<term><varname>timeout</varname></term>
<listitem>
<para>
Set timeout in milliseconds for all database responses. Use
<literal>-1</literal> to wait forever. Can be overridden with
<function>MongoCursor::timeout</function>. This does not cause the
MongoDB server to cancel the operation; it only instructs the driver to
stop waiting for a response and throw a
<classname>MongoCursorTimeoutException</classname> after a set time.
</para>
</listitem>
</varlistentry>
</variablelist>
</section>
<section role="seealso">
&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
-->