<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>command_cursor – Tools for iterating over MongoDB command results — PyMongo 3.7.2 documentation</title> <link rel="stylesheet" href="../../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <script type="text/javascript" src="../../_static/language_data.js"></script> <script type="text/javascript" src="../../_static/sidebar.js"></script> <link rel="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="next" title="cursor – Tools for iterating over MongoDB query results" href="cursor.html" /> <link rel="prev" title="collection – Collection level operations" href="collection.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="cursor.html" title="cursor – Tools for iterating over MongoDB query results" accesskey="N">next</a> |</li> <li class="right" > <a href="collection.html" title="collection – Collection level operations" accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li> <li class="nav-item nav-item-2"><a href="index.html" accesskey="U"><code class="docutils literal notranslate"><span class="pre">pymongo</span></code> – Python driver for MongoDB</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-pymongo.command_cursor"> <span id="command-cursor-tools-for-iterating-over-mongodb-command-results"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">command_cursor</span></code> – Tools for iterating over MongoDB command results<a class="headerlink" href="#module-pymongo.command_cursor" title="Permalink to this headline">¶</a></h1> <p>CommandCursor class to iterate over command results.</p> <dl class="class"> <dt id="pymongo.command_cursor.CommandCursor"> <em class="property">class </em><code class="descclassname">pymongo.command_cursor.</code><code class="descname">CommandCursor</code><span class="sig-paren">(</span><em>collection</em>, <em>cursor_info</em>, <em>address</em>, <em>retrieved=0</em>, <em>batch_size=0</em>, <em>max_await_time_ms=None</em>, <em>session=None</em>, <em>explicit_session=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.command_cursor.CommandCursor" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new command cursor.</p> <p>The parameter ‘retrieved’ is unused.</p> <dl class="attribute"> <dt id="pymongo.command_cursor.CommandCursor.address"> <code class="descname">address</code><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.address" title="Permalink to this definition">¶</a></dt> <dd><p>The (host, port) of the server used, or None.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.command_cursor.CommandCursor.alive"> <code class="descname">alive</code><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.alive" title="Permalink to this definition">¶</a></dt> <dd><p>Does this cursor have the potential to return more data?</p> <p>Even if <a class="reference internal" href="#pymongo.command_cursor.CommandCursor.alive" title="pymongo.command_cursor.CommandCursor.alive"><code class="xref py py-attr docutils literal notranslate"><span class="pre">alive</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="#pymongo.command_cursor.CommandCursor.next" title="pymongo.command_cursor.CommandCursor.next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> can raise <code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code>. Best to use a for loop:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">collection</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">pipeline</span><span class="p">):</span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><a class="reference internal" href="#pymongo.command_cursor.CommandCursor.alive" title="pymongo.command_cursor.CommandCursor.alive"><code class="xref py py-attr docutils literal notranslate"><span class="pre">alive</span></code></a> can be True while iterating a cursor from a failed server. In this case <a class="reference internal" href="#pymongo.command_cursor.CommandCursor.alive" title="pymongo.command_cursor.CommandCursor.alive"><code class="xref py py-attr docutils literal notranslate"><span class="pre">alive</span></code></a> will return False after <a class="reference internal" href="#pymongo.command_cursor.CommandCursor.next" title="pymongo.command_cursor.CommandCursor.next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> fails to retrieve the next batch of results from the server.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.command_cursor.CommandCursor.batch_size"> <code class="descname">batch_size</code><span class="sig-paren">(</span><em>batch_size</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.batch_size" title="Permalink to this definition">¶</a></dt> <dd><p>Limits the number of documents returned in one batch. Each batch requires a round trip to the server. It can be adjusted to optimize performance and limit data transfer.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">batch_size can not override MongoDB’s internal limits on the amount of data it will return to the client in a single batch (i.e if you set batch size to 1,000,000,000, MongoDB will currently only return 4-16MB of results per batch).</p> </div> <p>Raises <code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code> if <cite>batch_size</cite> is not an integer. Raises <code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code> if <cite>batch_size</cite> is less than <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><cite>batch_size</cite>: The size of each batch of results requested.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="pymongo.command_cursor.CommandCursor.close"> <code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.close" title="Permalink to this definition">¶</a></dt> <dd><p>Explicitly close / kill this cursor.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.command_cursor.CommandCursor.cursor_id"> <code class="descname">cursor_id</code><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.cursor_id" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the id of the cursor.</p> </dd></dl> <dl class="method"> <dt id="pymongo.command_cursor.CommandCursor.next"> <code class="descname">next</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.next" title="Permalink to this definition">¶</a></dt> <dd><p>Advance the cursor.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.command_cursor.CommandCursor.session"> <code class="descname">session</code><a class="headerlink" href="#pymongo.command_cursor.CommandCursor.session" title="Permalink to this definition">¶</a></dt> <dd><p>The cursor’s <a class="reference internal" href="client_session.html#pymongo.client_session.ClientSession" title="pymongo.client_session.ClientSession"><code class="xref py py-class docutils literal notranslate"><span class="pre">ClientSession</span></code></a>, or None.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.command_cursor.RawBatchCommandCursor"> <em class="property">class </em><code class="descclassname">pymongo.command_cursor.</code><code class="descname">RawBatchCommandCursor</code><span class="sig-paren">(</span><em>collection</em>, <em>cursor_info</em>, <em>address</em>, <em>retrieved=0</em>, <em>batch_size=0</em>, <em>max_await_time_ms=None</em>, <em>session=None</em>, <em>explicit_session=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.command_cursor.RawBatchCommandCursor" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new cursor / iterator over raw batches of BSON data.</p> <p>Should not be called directly by application developers - see <a class="reference internal" href="collection.html#pymongo.collection.Collection.aggregate_raw_batches" title="pymongo.collection.Collection.aggregate_raw_batches"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aggregate_raw_batches()</span></code></a> instead.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="admonition-title">The MongoDB documentation on</p> <p class="last"><a class="reference external" href="http://dochub.mongodb.org/core/cursors" name="pymongo.command_cursor.RawBatchCommandCursor"><em>cursors</em></a></p> </div> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="collection.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">collection</span></code> – Collection level operations</a></p> <h4>Next topic</h4> <p class="topless"><a href="cursor.html" title="next chapter"><code class="docutils literal notranslate"><span class="pre">cursor</span></code> – Tools for iterating over MongoDB query results</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/api/pymongo/command_cursor.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="cursor.html" title="cursor – Tools for iterating over MongoDB query results" >next</a> |</li> <li class="right" > <a href="collection.html" title="collection – Collection level operations" >previous</a> |</li> <li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li> <li class="nav-item nav-item-2"><a href="index.html" ><code class="docutils literal notranslate"><span class="pre">pymongo</span></code> – Python driver for MongoDB</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright MongoDB, Inc. 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc. </div> </body> </html>