php-doc-en/reference/mongo/mongocollection/aggregatecursor.xml
2015-01-15 21:55:38 +00:00

232 lines
7.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.aggregatecursor" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::aggregateCursor</refname>
<refpurpose>Execute an aggregation pipeline command and retrieve results through a cursor</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>MongoCommandCursor</type><methodname>MongoCollection::aggregateCursor</methodname>
<methodparam><type>array</type><parameter>command</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter></methodparam>
</methodsynopsis>
<para>
With this method you can execute Aggregation Framework pipelines and
retrieve the results through a cursor, instead of getting just one document
back as you would with <methodname>MongoCollection::aggregate</methodname>.
This method returns a <classname>MongoCommandCursor</classname> object.
This cursor object implements the <classname>Iterator</classname> interface
just like the <classname>MongoCursor</classname> objects that are returned
by the <methodname>MongoCollection::find</methodname> method.
</para>
<note>
<simpara>
The resulting <classname>MongoCommandCursor</classname> will inherit this
collection's read preference.
<methodname>MongoCommandCursor::setReadPreference</methodname> may be used
to change the read preference before iterating on the cursor.
</simpara>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>pipeline</parameter>
</term>
<listitem>
<para>
The Aggregation Framework pipeline to execute.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<para>Options for the aggregation command. Valid options include:</para>
<itemizedlist>
<listitem>
<para><literal>"allowDiskUse"</literal></para>
<para>Allow aggregation stages to write to temporary files</para>
</listitem>
<listitem>
<para><literal>"cursor"</literal></para>
<para>
It is possible to configure how many initial documents the server
should return with the first result set. The default initial batch size
is <literal>101</literal>. You can change it by adding the
<literal>batchSize</literal> option:
</para>
<programlisting role="php">
<![CDATA[
<?php
$collection->aggregateCursor(
$pipeline,
[ "cursor" => [ "batchSize" => 4 ] ]
);
]]>
</programlisting>
<para>
This option only configures the size of the first batch. To configure
the size of future batches, please use the
<methodname>MongoCommandCursor::batchSize</methodname> method on the
returned <classname>MongoCommandCursor</classname> object.
</para>
</listitem>
<listitem>
<para><literal>"explain"</literal></para>
<para>
Return information on the processing of the pipeline. This option may
cause the command to return a result document that is unsuitable for
constructing a <classname>MongoCommandCursor</classname>. If you need
to use this option, you should consider using
<methodname>MongoCollection::aggregate</methodname>.
</para>
</listitem>
&mongo.command.parameters.maxtimems;
</itemizedlist>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a <classname>MongoCommandCursor</classname> object. Because this
implements the <classname>Iterator</classname> interface you can
iterate over each of the results as returned by the command query. The
<classname>MongoCommandCursor</classname> also implements the
<classname>MongoCursorInterface</classname> interface which adds the
<methodname>MongoCommandCursor::batchSize</methodname>,
<methodname>MongoCommandCursor::dead</methodname>,
<methodname>MongoCommandCursor::info</methodname> methods.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoCollection::aggregateCursor</function> example</title>
<para>
Finding all of the distinct values for a key.
</para>
<programlisting role="php">
<![CDATA[
<?php
$m = new MongoClient;
$db = $m->test;
$people = $db->people;
$people->drop();
$people->insert(array("name" => "Joe", "points" => 4));
$people->insert(array("name" => "Molly", "points" => 43));
$people->insert(array("name" => "Sally", "points" => 22));
$people->insert(array("name" => "Joe", "points" => 22));
$people->insert(array("name" => "Molly", "points" => 87));
$ages = $people->aggregateCursor( [
[ '$group' => [ '_id' => '$name', 'points' => [ '$sum' => '$points' ] ] ],
[ '$sort' => [ 'points' => -1 ] ],
] );
foreach ($ages as $person) {
echo "{$person['_id']}: {$person['points']}\n";
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
Molly: 130
Joe: 26
Sally: 22
</screen>
</example>
<example>
<title><function>MongoCollection::aggregateCursor</function>
example with different initial batch size</title>
<para>
Finding all of the distinct values for a key.
</para>
<programlisting role="php">
<![CDATA[
<?php
$m = new MongoClient;
$db = $m->test;
$people = $db->people;
$people->drop();
/* Insert some sample data */
$people->insert(array("name" => "Joe", "points" => 4));
$people->insert(array("name" => "Molly", "points" => 43));
$people->insert(array("name" => "Sally", "points" => 22));
$people->insert(array("name" => "Joe", "points" => 22));
$people->insert(array("name" => "Molly", "points" => 87));
/* Run the command cursor */
$ages = $people->aggregateCursor(
[
[ '$group' => [ '_id' => '$name', 'points' => [ '$sum' => '$points' ] ] ],
[ '$sort' => [ 'points' => -1 ] ],
],
[ "cursor" => [ "batchSize" => 4 ] ]
);
foreach ($ages as $person) {
echo "{$person['_id']}: {$person['points']}\n";
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
Molly: 130
Joe: 26
Sally: 22
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><methodname>MongoDB::command</methodname></member>
<member><classname>MongoCommandCursor</classname></member>
<member><methodname>MongoCommandCursor::batchSize</methodname></member>
<member><methodname>MongoCollection::aggregate</methodname></member>
<member>The MongoDB <link xlink:href="&url.mongodb.docs.aggregation;">aggregation framework</link></member>
</simplelist>
</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
-->