<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Limits the number of elements returned in one batch.</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.awaitdata.html">MongoCursor::awaitData</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.construct.html">MongoCursor::__construct</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocursor.batchsize" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCursor::batchSize</h1> <p class="verinfo">(PECL mongo >=1.0.11)</p><p class="refpurpose"><span class="refname">MongoCursor::batchSize</span> — <span class="dc-title">Limits the number of elements returned in one batch.</span></p> </div> <div class="refsect1 description" id="refsect1-mongocursor.batchsize-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.mongocursor.html" class="type MongoCursor">MongoCursor</a></span> <span class="methodname"><strong>MongoCursor::batchSize</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$batchSize</code></span> )</div> <p class="para rdfs-comment"> A cursor typically fetches a batch of result objects and store them locally. This method sets the batchSize value to configure the amount of documents retrieved from the server in one data packet. However, it will never return more documents than fit in the max batch size limit (usually 4MB). </p> </div> <div class="refsect1 parameters" id="refsect1-mongocursor.batchsize-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"> <em><code class="parameter">batchSize</code></em> </span> <dd> <p class="para"> The number of results to return per batch. Each batch requires a round-trip to the server. </p> <p class="para"> If <em><code class="parameter">batchSize</code></em> is <em class="emphasis">2 or more</em>, it represents the size of each batch of objects retrieved. It can be adjusted to optimize performance and limit data transfer. </p> <p class="para"> If <em><code class="parameter">batchSize</code></em> is <em>1</em> or negative, it will limit of number returned documents to the absolute value of batchSize, and the cursor will be closed. For example if batchSize is <em>-10</em>, then the server will return a maximum of 10 documents and as many as can fit in 4MB, then close the cursor. </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> A <em><code class="parameter">batchSize</code></em> of <em>1</em> is special, and means the same as <em>-1</em>, i.e. a value of <em>1</em> makes the cursor only capable of returning <em class="emphasis">one</em> document. </p> </div> <p class="para"> Note that this feature is different from <span class="function"><a href="mongocursor.limit.html" class="function">MongoCursor::limit()</a></span> in that documents must fit within a maximum size, and it removes the need to send a request to close the cursor server-side. The batch size can be changed even after a cursor is iterated, in which case the setting will apply on the next batch retrieval. </p> <p class="para"> This cannot override MongoDB's limit on the amount of data it will return to the client (i.e., if you set batch size to 1,000,000,000, MongoDB will still only return 4-16MB of results per batch). </p> <p class="para"> To ensure consistent behavior, the rules of <span class="function"><strong>MongoCursor::batchSize()</strong></span> and <span class="function"><a href="mongocursor.limit.html" class="function">MongoCursor::limit()</a></span> behave a little complex but work "as expected". The rules are: hard limits override soft limits with preference given to <span class="function"><a href="mongocursor.limit.html" class="function">MongoCursor::limit()</a></span> over <span class="function"><strong>MongoCursor::batchSize()</strong></span>. After that, whichever is set and lower than the other will take precedence. See below. section for some examples. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocursor.batchsize-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns this cursor. </p> </div> <div class="refsect1 examples" id="refsect1-mongocursor.batchsize-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1477"> <p><strong>Example #1 <span class="function"><strong>MongoCursor::batchSize()</strong></span> and combinations with <span class="function"><a href="mongocursor.limit.html" class="function">MongoCursor::limit()</a></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// one batch, at most 10 items. The -10 makes the server to return 10 items,<br />// and then remove the cursor.<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">limit</span><span style="color: #007700">(</span><span style="color: #0000BB">20</span><span style="color: #007700">)-></span><span style="color: #0000BB">batchSize</span><span style="color: #007700">(-</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// first batch: at most 10 items<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">limit</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// first batch: at most 10 items<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">limit</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">)-></span><span style="color: #0000BB">batchSize</span><span style="color: #007700">(</span><span style="color: #0000BB">20</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// results are fetched in batches of 10 each, with a maximum of 20 items<br />// returned (that means two batches of 10).<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">limit</span><span style="color: #007700">(</span><span style="color: #0000BB">20</span><span style="color: #007700">)-></span><span style="color: #0000BB">batchSize</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// results are fetched in batches of 7 each, with a maximum of 30 items<br />// returned (that means that the driver requests 4 batches of 7, and one batch<br />// of 2).<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">limit</span><span style="color: #007700">(</span><span style="color: #0000BB">30</span><span style="color: #007700">)-></span><span style="color: #0000BB">batchSize</span><span style="color: #007700">(</span><span style="color: #0000BB">7</span><span style="color: #007700">)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mongocursor.batchsize-seealso"> <h3 class="title">See Also</h3> <p class="para"> MongoDB core docs on <a href="http://docs.mongodb.org/manual/reference/method/cursor.batchSize/" class="link external">» batchSize</a>. </p> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="mongocursor.limit.html" class="function" rel="rdfs-seeAlso">MongoCursor::limit()</a> - Limits the number of results returned</span></li> </ul> </p> </div> <div class="refsect1 changelog" id="refsect1-mongocursor.batchsize-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>1.4.5</td> <td> <p class="para"> Before 1.4.5, this method would throw an <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a> if the cursor had already started iterating. </p> </td> </tr> </tbody> </table> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.awaitdata.html">MongoCursor::awaitData</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.construct.html">MongoCursor::__construct</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>