<!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>collection – Collection level operations — 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="command_cursor – Tools for iterating over MongoDB command results" href="command_cursor.html" /> <link rel="prev" title="collation – Tools for working with collations." href="collation.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="command_cursor.html" title="command_cursor – Tools for iterating over MongoDB command results" accesskey="N">next</a> |</li> <li class="right" > <a href="collation.html" title="collation – Tools for working with collations." 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.collection"> <span id="collection-collection-level-operations"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">collection</span></code> – Collection level operations<a class="headerlink" href="#module-pymongo.collection" title="Permalink to this headline">¶</a></h1> <p>Collection level utilities for Mongo.</p> <dl class="data"> <dt id="pymongo.ASCENDING"> <code class="descclassname">pymongo.</code><code class="descname">ASCENDING</code><em class="property"> = 1</em><a class="headerlink" href="#pymongo.ASCENDING" title="Permalink to this definition">¶</a></dt> <dd><p>Ascending sort order.</p> </dd></dl> <dl class="data"> <dt id="pymongo.DESCENDING"> <code class="descclassname">pymongo.</code><code class="descname">DESCENDING</code><em class="property"> = -1</em><a class="headerlink" href="#pymongo.DESCENDING" title="Permalink to this definition">¶</a></dt> <dd><p>Descending sort order.</p> </dd></dl> <dl class="data"> <dt id="pymongo.GEO2D"> <code class="descclassname">pymongo.</code><code class="descname">GEO2D</code><em class="property"> = '2d'</em><a class="headerlink" href="#pymongo.GEO2D" title="Permalink to this definition">¶</a></dt> <dd><p>Index specifier for a 2-dimensional <a class="reference external" href="http://docs.mongodb.org/manual/core/2d/">geospatial index</a>.</p> </dd></dl> <dl class="data"> <dt id="pymongo.GEOHAYSTACK"> <code class="descclassname">pymongo.</code><code class="descname">GEOHAYSTACK</code><em class="property"> = 'geoHaystack'</em><a class="headerlink" href="#pymongo.GEOHAYSTACK" title="Permalink to this definition">¶</a></dt> <dd><p>Index specifier for a 2-dimensional <a class="reference external" href="http://docs.mongodb.org/manual/core/geohaystack/">haystack index</a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1.</span></p> </div> </dd></dl> <dl class="data"> <dt id="pymongo.GEOSPHERE"> <code class="descclassname">pymongo.</code><code class="descname">GEOSPHERE</code><em class="property"> = '2dsphere'</em><a class="headerlink" href="#pymongo.GEOSPHERE" title="Permalink to this definition">¶</a></dt> <dd><p>Index specifier for a <a class="reference external" href="http://docs.mongodb.org/manual/core/2dsphere/">spherical geospatial index</a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> </dd></dl> <dl class="data"> <dt id="pymongo.HASHED"> <code class="descclassname">pymongo.</code><code class="descname">HASHED</code><em class="property"> = 'hashed'</em><a class="headerlink" href="#pymongo.HASHED" title="Permalink to this definition">¶</a></dt> <dd><p>Index specifier for a <a class="reference external" href="http://docs.mongodb.org/manual/core/index-hashed/">hashed index</a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> </dd></dl> <dl class="data"> <dt id="pymongo.TEXT"> <code class="descclassname">pymongo.</code><code class="descname">TEXT</code><em class="property"> = 'text'</em><a class="headerlink" href="#pymongo.TEXT" title="Permalink to this definition">¶</a></dt> <dd><p>Index specifier for a <a class="reference external" href="http://docs.mongodb.org/manual/core/index-text/">text index</a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.1.</span></p> </div> </dd></dl> <dl class="class"> <dt id="pymongo.collection.ReturnDocument"> <em class="property">class </em><code class="descclassname">pymongo.collection.</code><code class="descname">ReturnDocument</code><a class="headerlink" href="#pymongo.collection.ReturnDocument" title="Permalink to this definition">¶</a></dt> <dd><p>An enum used with <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_replace" title="pymongo.collection.Collection.find_one_and_replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_replace()</span></code></a> and <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_update()</span></code></a>.</p> <dl class="attribute"> <dt id="pymongo.collection.ReturnDocument.BEFORE"> <code class="descname">BEFORE</code><a class="headerlink" href="#pymongo.collection.ReturnDocument.BEFORE" title="Permalink to this definition">¶</a></dt> <dd><p>Return the original document before it was updated/replaced, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no document matches the query.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.ReturnDocument.AFTER"> <code class="descname">AFTER</code><a class="headerlink" href="#pymongo.collection.ReturnDocument.AFTER" title="Permalink to this definition">¶</a></dt> <dd><p>Return the updated/replaced or inserted document.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.collection.Collection"> <em class="property">class </em><code class="descclassname">pymongo.collection.</code><code class="descname">Collection</code><span class="sig-paren">(</span><em>database</em>, <em>name</em>, <em>create=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection" title="Permalink to this definition">¶</a></dt> <dd><p>Get / create a Mongo collection.</p> <p>Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if <cite>name</cite> is not an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code> in python 3). Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidName</span></code></a> if <cite>name</cite> is not a valid collection name. Any additional keyword arguments will be used as options passed to the create command. See <a class="reference internal" href="database.html#pymongo.database.Database.create_collection" title="pymongo.database.Database.create_collection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_collection()</span></code></a> for valid options.</p> <p>If <cite>create</cite> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <cite>collation</cite> is specified, or any additional keyword arguments are present, a <code class="docutils literal notranslate"><span class="pre">create</span></code> command will be sent, using <code class="docutils literal notranslate"><span class="pre">session</span></code> if specified. Otherwise, a <code class="docutils literal notranslate"><span class="pre">create</span></code> command will not be sent and the collection will be created implicitly on first use. The optional <code class="docutils literal notranslate"><span class="pre">session</span></code> argument is <em>only</em> used for the <code class="docutils literal notranslate"><span class="pre">create</span></code> command, it is not associated with the collection afterward.</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>database</cite>: the database to get a collection from</li> <li><cite>name</cite>: the name of the collection to get</li> <li><cite>create</cite> (optional): if <code class="docutils literal notranslate"><span class="pre">True</span></code>, force collection creation even without options being set</li> <li><cite>codec_options</cite> (optional): An instance of <a class="reference internal" href="../bson/codec_options.html#bson.codec_options.CodecOptions" title="bson.codec_options.CodecOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecOptions</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) database.codec_options is used.</li> <li><cite>read_preference</cite> (optional): The read preference to use. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) database.read_preference is used.</li> <li><cite>write_concern</cite> (optional): An instance of <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteConcern</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) database.write_concern is used.</li> <li><cite>read_concern</cite> (optional): An instance of <a class="reference internal" href="read_concern.html#pymongo.read_concern.ReadConcern" title="pymongo.read_concern.ReadConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadConcern</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) database.read_concern is used.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. If a collation is provided, it will be passed to the create collection command. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>session</cite> (optional): a <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> that is used with the create collection command</li> <li><cite>**kwargs</cite> (optional): additional keyword arguments will be passed as options for the create collection command</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added the read_concern option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Added the codec_options, read_preference, and write_concern options. Removed the uuid_subtype attribute. <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> no longer returns an instance of <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> for attribute names with leading underscores. You must use dict-style lookups instead::</p> <blockquote> <div>collection[‘__my_collection__’]</div></blockquote> <p>Not:</p> <blockquote> <div>collection.__my_collection__</div></blockquote> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: options</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.1: </span>uuid_subtype attribute</p> </div> <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/collections" name="pymongo.collection.Collection"><em>collections</em></a></p> </div> <dl class="describe"> <dt> <code class="descname">c[name] || c.name</code></dt> <dd><p>Get the <cite>name</cite> sub-collection of <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> <cite>c</cite>.</p> <p>Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidName</span></code></a> if an invalid collection name is used.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.full_name"> <code class="descname">full_name</code><a class="headerlink" href="#pymongo.collection.Collection.full_name" title="Permalink to this definition">¶</a></dt> <dd><p>The full name of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</p> <p>The full name is of the form <cite>database_name.collection_name</cite>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.collection.Collection.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.database"> <code class="descname">database</code><a class="headerlink" href="#pymongo.collection.Collection.database" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="database.html#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> that this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> is a part of.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.codec_options"> <code class="descname">codec_options</code><a class="headerlink" href="#pymongo.collection.Collection.codec_options" title="Permalink to this definition">¶</a></dt> <dd><p>Read only access to the <a class="reference internal" href="../bson/codec_options.html#bson.codec_options.CodecOptions" title="bson.codec_options.CodecOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecOptions</span></code></a> of this instance.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.read_preference"> <code class="descname">read_preference</code><a class="headerlink" href="#pymongo.collection.Collection.read_preference" title="Permalink to this definition">¶</a></dt> <dd><p>Read only access to the read preference of this instance.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>The <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> attribute is now read only.</p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.write_concern"> <code class="descname">write_concern</code><a class="headerlink" href="#pymongo.collection.Collection.write_concern" title="Permalink to this definition">¶</a></dt> <dd><p>Read only access to the <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteConcern</span></code></a> of this instance.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> attribute is now read only.</p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.collection.Collection.read_concern"> <code class="descname">read_concern</code><a class="headerlink" href="#pymongo.collection.Collection.read_concern" title="Permalink to this definition">¶</a></dt> <dd><p>Read only access to the <a class="reference internal" href="read_concern.html#pymongo.read_concern.ReadConcern" title="pymongo.read_concern.ReadConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadConcern</span></code></a> of this instance.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.2.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.with_options"> <code class="descname">with_options</code><span class="sig-paren">(</span><em>codec_options=None</em>, <em>read_preference=None</em>, <em>write_concern=None</em>, <em>read_concern=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.with_options" title="Permalink to this definition">¶</a></dt> <dd><p>Get a clone of this collection changing the specified settings.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">coll1</span><span class="o">.</span><span class="n">read_preference</span> <span class="go">Primary()</span> <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">ReadPreference</span> <span class="gp">>>> </span><span class="n">coll2</span> <span class="o">=</span> <span class="n">coll1</span><span class="o">.</span><span class="n">with_options</span><span class="p">(</span><span class="n">read_preference</span><span class="o">=</span><span class="n">ReadPreference</span><span class="o">.</span><span class="n">SECONDARY</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">coll1</span><span class="o">.</span><span class="n">read_preference</span> <span class="go">Primary()</span> <span class="gp">>>> </span><span class="n">coll2</span><span class="o">.</span><span class="n">read_preference</span> <span class="go">Secondary(tag_sets=None)</span> </pre></div> </div> <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>codec_options</cite> (optional): An instance of <a class="reference internal" href="../bson/codec_options.html#bson.codec_options.CodecOptions" title="bson.codec_options.CodecOptions"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecOptions</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) the <a class="reference internal" href="#pymongo.collection.Collection.codec_options" title="pymongo.collection.Collection.codec_options"><code class="xref py py-attr docutils literal notranslate"><span class="pre">codec_options</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> is used.</li> <li><cite>read_preference</cite> (optional): The read preference to use. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> is used. See <a class="reference internal" href="read_preferences.html#module-pymongo.read_preferences" title="pymongo.read_preferences: Utilities for choosing which member of a replica set to read from."><code class="xref py py-mod docutils literal notranslate"><span class="pre">read_preferences</span></code></a> for options.</li> <li><cite>write_concern</cite> (optional): An instance of <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteConcern</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) the <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> is used.</li> <li><cite>read_concern</cite> (optional): An instance of <a class="reference internal" href="read_concern.html#pymongo.read_concern.ReadConcern" title="pymongo.read_concern.ReadConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReadConcern</span></code></a>. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) the <a class="reference internal" href="#pymongo.collection.Collection.read_concern" title="pymongo.collection.Collection.read_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_concern</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> is used.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.bulk_write"> <code class="descname">bulk_write</code><span class="sig-paren">(</span><em>requests</em>, <em>ordered=True</em>, <em>bypass_document_validation=False</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.bulk_write" title="Permalink to this definition">¶</a></dt> <dd><p>Send a batch of write operations to the server.</p> <p>Requests are passed as a list of write operation instances ( <a class="reference internal" href="operations.html#pymongo.operations.InsertOne" title="pymongo.operations.InsertOne"><code class="xref py py-class docutils literal notranslate"><span class="pre">InsertOne</span></code></a>, <a class="reference internal" href="operations.html#pymongo.operations.UpdateOne" title="pymongo.operations.UpdateOne"><code class="xref py py-class docutils literal notranslate"><span class="pre">UpdateOne</span></code></a>, <a class="reference internal" href="operations.html#pymongo.operations.UpdateMany" title="pymongo.operations.UpdateMany"><code class="xref py py-class docutils literal notranslate"><span class="pre">UpdateMany</span></code></a>, <a class="reference internal" href="operations.html#pymongo.operations.ReplaceOne" title="pymongo.operations.ReplaceOne"><code class="xref py py-class docutils literal notranslate"><span class="pre">ReplaceOne</span></code></a>, <a class="reference internal" href="operations.html#pymongo.operations.DeleteOne" title="pymongo.operations.DeleteOne"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeleteOne</span></code></a>, or <a class="reference internal" href="operations.html#pymongo.operations.DeleteMany" title="pymongo.operations.DeleteMany"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeleteMany</span></code></a>).</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634ef')}</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634f0')}</span> <span class="gp">>>> </span><span class="c1"># DeleteMany, UpdateOne, and UpdateMany are also available.</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">InsertOne</span><span class="p">,</span> <span class="n">DeleteOne</span><span class="p">,</span> <span class="n">ReplaceOne</span> <span class="gp">>>> </span><span class="n">requests</span> <span class="o">=</span> <span class="p">[</span><span class="n">InsertOne</span><span class="p">({</span><span class="s1">'y'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}),</span> <span class="n">DeleteOne</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}),</span> <span class="gp">... </span> <span class="n">ReplaceOne</span><span class="p">({</span><span class="s1">'w'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'z'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">upsert</span><span class="o">=</span><span class="kc">True</span><span class="p">)]</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">bulk_write</span><span class="p">(</span><span class="n">requests</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">inserted_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">deleted_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">upserted_ids</span> <span class="go">{2: ObjectId('54f62ee28891e756a6e1abd5')}</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634f0')}</span> <span class="go">{u'y': 1, u'_id': ObjectId('54f62ee2fba5226811f634f1')}</span> <span class="go">{u'z': 1, u'_id': ObjectId('54f62ee28891e756a6e1abd5')}</span> </pre></div> </div> <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 simple"> <li><cite>requests</cite>: A list of write operations (see examples above).</li> <li><cite>ordered</cite> (optional): If <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) requests will be performed on the server serially, in the order provided. If an error occurs all remaining operations are aborted. If <code class="docutils literal notranslate"><span class="pre">False</span></code> requests will be performed on the server in arbitrary order, possibly in parallel, and all operations will be attempted.</li> <li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">An instance of <a class="reference internal" href="results.html#pymongo.results.BulkWriteResult" title="pymongo.results.BulkWriteResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">BulkWriteResult</span></code></a>.</p> </td> </tr> </tbody> </table> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="../../faq.html#writes-and-ids"><span class="std std-ref">Why does PyMongo add an _id field to all of my documents?</span></a></p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.insert_one"> <code class="descname">insert_one</code><span class="sig-paren">(</span><em>document</em>, <em>bypass_document_validation=False</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.insert_one" title="Permalink to this definition">¶</a></dt> <dd><p>Insert a single document.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">insert_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">inserted_id</span> <span class="go">ObjectId('54f112defba522406c9cc208')</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f112defba522406c9cc208')}</span> </pre></div> </div> <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 simple"> <li><cite>document</cite>: The document to insert. Must be a mutable mapping type. If the document does not have an _id field one will be added automatically.</li> <li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple"> <li>An instance of <a class="reference internal" href="results.html#pymongo.results.InsertOneResult" title="pymongo.results.InsertOneResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">InsertOneResult</span></code></a>.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="../../faq.html#writes-and-ids"><span class="std std-ref">Why does PyMongo add an _id field to all of my documents?</span></a></p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.insert_many"> <code class="descname">insert_many</code><span class="sig-paren">(</span><em>documents</em>, <em>ordered=True</em>, <em>bypass_document_validation=False</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.insert_many" title="Permalink to this definition">¶</a></dt> <dd><p>Insert an iterable of documents.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({})</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">insert_many</span><span class="p">([{</span><span class="s1">'x'</span><span class="p">:</span> <span class="n">i</span><span class="p">}</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)])</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">inserted_ids</span> <span class="go">[ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({})</span> <span class="go">2</span> </pre></div> </div> <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 simple"> <li><cite>documents</cite>: A iterable of documents to insert.</li> <li><cite>ordered</cite> (optional): If <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) documents will be inserted on the server serially, in the order provided. If an error occurs all remaining inserts are aborted. If <code class="docutils literal notranslate"><span class="pre">False</span></code>, documents will be inserted on the server in arbitrary order, possibly in parallel, and all document inserts will be attempted.</li> <li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">An instance of <a class="reference internal" href="results.html#pymongo.results.InsertManyResult" title="pymongo.results.InsertManyResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">InsertManyResult</span></code></a>.</p> </td> </tr> </tbody> </table> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="../../faq.html#writes-and-ids"><span class="std std-ref">Why does PyMongo add an _id field to all of my documents?</span></a></p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.replace_one"> <code class="descname">replace_one</code><span class="sig-paren">(</span><em>filter</em>, <em>replacement</em>, <em>upsert=False</em>, <em>bypass_document_validation=False</em>, <em>collation=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.replace_one" title="Permalink to this definition">¶</a></dt> <dd><p>Replace a single document matching the filter.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f4c5befba5220aa4d6dee7')}</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">replace_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'y'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'y': 1, u'_id': ObjectId('54f4c5befba5220aa4d6dee7')}</span> </pre></div> </div> <p>The <em>upsert</em> option can be used to insert a new document if a matching document does not exist.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">replace_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="kc">True</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">upserted_id</span> <span class="go">ObjectId('54f11e5c8891e756a6e1abd4')</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f11e5c8891e756a6e1abd4')}</span> </pre></div> </div> <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 simple"> <li><cite>filter</cite>: A query that matches the document to replace.</li> <li><cite>replacement</cite>: The new document.</li> <li><cite>upsert</cite> (optional): If <code class="docutils literal notranslate"><span class="pre">True</span></code>, perform an insert if no documents match the filter.</li> <li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple"> <li>An instance of <a class="reference internal" href="results.html#pymongo.results.UpdateResult" title="pymongo.results.UpdateResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">UpdateResult</span></code></a>.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.update_one"> <code class="descname">update_one</code><span class="sig-paren">(</span><em>filter</em>, <em>update</em>, <em>upsert=False</em>, <em>bypass_document_validation=False</em>, <em>collation=None</em>, <em>array_filters=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.update_one" title="Permalink to this definition">¶</a></dt> <dd><p>Update a single document matching the filter.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': 0}</span> <span class="go">{u'x': 1, u'_id': 1}</span> <span class="go">{u'x': 1, u'_id': 2}</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">update_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">3</span><span class="p">}})</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 4, u'_id': 0}</span> <span class="go">{u'x': 1, u'_id': 1}</span> <span class="go">{u'x': 1, u'_id': 2}</span> </pre></div> </div> <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 simple"> <li><cite>filter</cite>: A query that matches the document to update.</li> <li><cite>update</cite>: The modifications to apply.</li> <li><cite>upsert</cite> (optional): If <code class="docutils literal notranslate"><span class="pre">True</span></code>, perform an insert if no documents match the filter.</li> <li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>array_filters</cite> (optional): A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple"> <li>An instance of <a class="reference internal" href="results.html#pymongo.results.UpdateResult" title="pymongo.results.UpdateResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">UpdateResult</span></code></a>.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added the <cite>array_filters</cite> and <code class="docutils literal notranslate"><span class="pre">session</span></code> parameters.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.update_many"> <code class="descname">update_many</code><span class="sig-paren">(</span><em>filter</em>, <em>update</em>, <em>upsert=False</em>, <em>array_filters=None</em>, <em>bypass_document_validation=False</em>, <em>collation=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.update_many" title="Permalink to this definition">¶</a></dt> <dd><p>Update one or more documents that match the filter.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': 0}</span> <span class="go">{u'x': 1, u'_id': 1}</span> <span class="go">{u'x': 1, u'_id': 2}</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">update_many</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">3</span><span class="p">}})</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span> <span class="go">3</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span> <span class="go">3</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 4, u'_id': 0}</span> <span class="go">{u'x': 4, u'_id': 1}</span> <span class="go">{u'x': 4, u'_id': 2}</span> </pre></div> </div> <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 simple"> <li><cite>filter</cite>: A query that matches the documents to update.</li> <li><cite>update</cite>: The modifications to apply.</li> <li><cite>upsert</cite> (optional): If <code class="docutils literal notranslate"><span class="pre">True</span></code>, perform an insert if no documents match the filter.</li> <li><cite>bypass_document_validation</cite> (optional): If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>array_filters</cite> (optional): A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple"> <li>An instance of <a class="reference internal" href="results.html#pymongo.results.UpdateResult" title="pymongo.results.UpdateResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">UpdateResult</span></code></a>.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">array_filters</span></code> and <code class="docutils literal notranslate"><span class="pre">session</span></code> parameters.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.delete_one"> <code class="descname">delete_one</code><span class="sig-paren">(</span><em>filter</em>, <em>collation=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.delete_one" title="Permalink to this definition">¶</a></dt> <dd><p>Delete a single document matching the filter.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">3</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">delete_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">deleted_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">2</span> </pre></div> </div> <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 simple"> <li><cite>filter</cite>: A query that matches the document to delete.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple"> <li>An instance of <a class="reference internal" href="results.html#pymongo.results.DeleteResult" title="pymongo.results.DeleteResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeleteResult</span></code></a>.</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.delete_many"> <code class="descname">delete_many</code><span class="sig-paren">(</span><em>filter</em>, <em>collation=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.delete_many" title="Permalink to this definition">¶</a></dt> <dd><p>Delete one or more documents matching the filter.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">3</span> <span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">delete_many</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">deleted_count</span> <span class="go">3</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">0</span> </pre></div> </div> <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 simple"> <li><cite>filter</cite>: A query that matches the documents to delete.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple"> <li>An instance of <a class="reference internal" href="results.html#pymongo.results.DeleteResult" title="pymongo.results.DeleteResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeleteResult</span></code></a>.</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.aggregate"> <code class="descname">aggregate</code><span class="sig-paren">(</span><em>pipeline</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.aggregate" title="Permalink to this definition">¶</a></dt> <dd><p>Perform an aggregation using the aggregation framework on this collection.</p> <p>All optional <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/aggregate">aggregate command</a> parameters should be passed as keyword arguments to this method. Valid options include, but are not limited to:</p> <blockquote> <div><ul class="simple"> <li><cite>allowDiskUse</cite> (bool): Enables writing to temporary files. When set to True, aggregation stages can write data to the _tmp subdirectory of the –dbpath directory. The default is False.</li> <li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow the operation to run in milliseconds.</li> <li><cite>batchSize</cite> (int): The maximum number of documents to return per batch. Ignored if the connected mongod or mongos does not support returning aggregate results using a cursor, or <cite>useCursor</cite> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>useCursor</cite> (bool): Deprecated. Will be removed in PyMongo 4.0.</li> </ul> </div></blockquote> <p>The <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aggregate()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>. Please note that using the <code class="docutils literal notranslate"><span class="pre">$out</span></code> pipeline stage requires a read preference of <a class="reference internal" href="read_preferences.html#pymongo.read_preferences.ReadPreference.PRIMARY" title="pymongo.read_preferences.ReadPreference.PRIMARY"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PRIMARY</span></code></a> (the default). The server will raise an error if the <code class="docutils literal notranslate"><span class="pre">$out</span></code> pipeline stage is used with any other read preference.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This method does not support the ‘explain’ option. Please use <a class="reference internal" href="database.html#pymongo.database.Database.command" title="pymongo.database.Database.command"><code class="xref py py-meth docutils literal notranslate"><span class="pre">command()</span></code></a> instead. An example is included in the <a class="reference internal" href="../../examples/aggregation.html#aggregate-examples"><span class="std std-ref">Aggregation Framework</span></a> documentation.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation when using MongoDB >= 3.4.</p> </div> <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 simple"> <li><cite>pipeline</cite>: a list of aggregation pipeline stages</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): See list of options above.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A <a class="reference internal" href="command_cursor.html#pymongo.command_cursor.CommandCursor" title="pymongo.command_cursor.CommandCursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">CommandCursor</span></code></a> over the result set.</p> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added the <cite>session</cite> parameter. Added the <cite>maxAwaitTimeMS</cite> option. Deprecated the <cite>useCursor</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4. Support the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>The <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aggregate()</span></code></a> method always returns a CommandCursor. The pipeline argument must be a list.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.7: </span>When the cursor option is used, return <a class="reference internal" href="command_cursor.html#pymongo.command_cursor.CommandCursor" title="pymongo.command_cursor.CommandCursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">CommandCursor</span></code></a> instead of <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.6: </span>Added cursor support.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3.</span></p> </div> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="../../examples/aggregation.html"><span class="doc">Aggregation Examples</span></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.aggregate_raw_batches"> <code class="descname">aggregate_raw_batches</code><span class="sig-paren">(</span><em>pipeline</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.aggregate_raw_batches" title="Permalink to this definition">¶</a></dt> <dd><p>Perform an aggregation and retrieve batches of raw BSON.</p> <p>Similar to the <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aggregate()</span></code></a> method but returns a <a class="reference internal" href="cursor.html#pymongo.cursor.RawBatchCursor" title="pymongo.cursor.RawBatchCursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawBatchCursor</span></code></a>.</p> <p>This example demonstrates how to work with raw batches, but in practice raw batches should be passed to an external library that can decode BSON into another data type, rather than used with PyMongo’s <a class="reference internal" href="../bson/index.html#module-bson" title="bson: BSON (Binary JSON) Encoding and Decoding"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bson</span></code></a> module.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">bson</span> <span class="gp">>>> </span><span class="n">cursor</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">aggregate_raw_batches</span><span class="p">([</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'$project'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'$multiply'</span><span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="s1">'$x'</span><span class="p">]}}}])</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">batch</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">bson</span><span class="o">.</span><span class="n">decode_all</span><span class="p">(</span><span class="n">batch</span><span class="p">))</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">aggregate_raw_batches does not support sessions.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.watch"> <code class="descname">watch</code><span class="sig-paren">(</span><em>pipeline=None</em>, <em>full_document='default'</em>, <em>resume_after=None</em>, <em>max_await_time_ms=None</em>, <em>batch_size=None</em>, <em>collation=None</em>, <em>start_at_operation_time=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.watch" title="Permalink to this definition">¶</a></dt> <dd><p>Watch changes on this collection.</p> <p>Performs an aggregation with an implicit initial <code class="docutils literal notranslate"><span class="pre">$changeStream</span></code> stage and returns a <a class="reference internal" href="change_stream.html#pymongo.change_stream.CollectionChangeStream" title="pymongo.change_stream.CollectionChangeStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">CollectionChangeStream</span></code></a> cursor which iterates over changes on this collection.</p> <p>Introduced in MongoDB 3.6.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">db</span><span class="o">.</span><span class="n">collection</span><span class="o">.</span><span class="n">watch</span><span class="p">()</span> <span class="k">as</span> <span class="n">stream</span><span class="p">:</span> <span class="k">for</span> <span class="n">change</span> <span class="ow">in</span> <span class="n">stream</span><span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="n">change</span><span class="p">)</span> </pre></div> </div> <p>The <a class="reference internal" href="change_stream.html#pymongo.change_stream.CollectionChangeStream" title="pymongo.change_stream.CollectionChangeStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">CollectionChangeStream</span></code></a> iterable blocks until the next change document is returned or an error is raised. If the <code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code> method encounters a network error when retrieving a batch from the server, it will automatically attempt to recreate the cursor such that no change events are missed. Any error encountered during the resume attempt indicates there may be an outage and will be raised.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="k">with</span> <span class="n">db</span><span class="o">.</span><span class="n">collection</span><span class="o">.</span><span class="n">watch</span><span class="p">(</span> <span class="p">[{</span><span class="s1">'$match'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'operationType'</span><span class="p">:</span> <span class="s1">'insert'</span><span class="p">}}])</span> <span class="k">as</span> <span class="n">stream</span><span class="p">:</span> <span class="k">for</span> <span class="n">insert_change</span> <span class="ow">in</span> <span class="n">stream</span><span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="n">insert_change</span><span class="p">)</span> <span class="k">except</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">PyMongoError</span><span class="p">:</span> <span class="c1"># The ChangeStream encountered an unrecoverable error or the</span> <span class="c1"># resume attempt failed to recreate the cursor.</span> <span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">'...'</span><span class="p">)</span> </pre></div> </div> <p>For a precise description of the resume process see the <a class="reference external" href="https://github.com/mongodb/specifications/blob/master/source/change-streams/change-streams.rst">change streams specification</a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Using this helper method is preferred to directly calling <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aggregate()</span></code></a> with a <code class="docutils literal notranslate"><span class="pre">$changeStream</span></code> stage, for the purpose of supporting resumability.</p> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">This Collection’s <a class="reference internal" href="#pymongo.collection.Collection.read_concern" title="pymongo.collection.Collection.read_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_concern</span></code></a> must be <code class="docutils literal notranslate"><span class="pre">ReadConcern("majority")</span></code> in order to use the <code class="docutils literal notranslate"><span class="pre">$changeStream</span></code> stage.</p> </div> <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 simple"> <li><cite>pipeline</cite> (optional): A list of aggregation pipeline stages to append to an initial <code class="docutils literal notranslate"><span class="pre">$changeStream</span></code> stage. Not all pipeline stages are valid after a <code class="docutils literal notranslate"><span class="pre">$changeStream</span></code> stage, see the MongoDB documentation on change streams for the supported stages.</li> <li><cite>full_document</cite> (optional): The fullDocument to pass as an option to the <code class="docutils literal notranslate"><span class="pre">$changeStream</span></code> stage. Allowed values: ‘default’, ‘updateLookup’. Defaults to ‘default’. When set to ‘updateLookup’, the change notification for partial updates will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.</li> <li><cite>resume_after</cite> (optional): The logical starting point for this change stream.</li> <li><cite>max_await_time_ms</cite> (optional): The maximum time in milliseconds for the server to wait for changes before responding to a getMore operation.</li> <li><cite>batch_size</cite> (optional): The maximum number of documents to return per batch.</li> <li><cite>collation</cite> (optional): The <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a> to use for the aggregation.</li> <li><cite>start_at_operation_time</cite> (optional): If provided, the resulting change stream will only return changes that occurred at or after the specified <a class="reference internal" href="../bson/timestamp.html#bson.timestamp.Timestamp" title="bson.timestamp.Timestamp"><code class="xref py py-class docutils literal notranslate"><span class="pre">Timestamp</span></code></a>. Requires MongoDB >= 4.0.</li> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A <a class="reference internal" href="change_stream.html#pymongo.change_stream.CollectionChangeStream" title="pymongo.change_stream.CollectionChangeStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">CollectionChangeStream</span></code></a> cursor.</p> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Added the <code class="docutils literal notranslate"><span class="pre">start_at_operation_time</span></code> parameter.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> <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/changeStreams" name="pymongo.collection.Collection.watch"><em>changeStreams</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find"> <code class="descname">find</code><span class="sig-paren">(</span><em>filter=None</em>, <em>projection=None</em>, <em>skip=0</em>, <em>limit=0</em>, <em>no_cursor_timeout=False</em>, <em>cursor_type=CursorType.NON_TAILABLE</em>, <em>sort=None</em>, <em>allow_partial_results=False</em>, <em>oplog_replay=False</em>, <em>modifiers=None</em>, <em>batch_size=0</em>, <em>manipulate=True</em>, <em>collation=None</em>, <em>hint=None</em>, <em>max_scan=None</em>, <em>max_time_ms=None</em>, <em>max=None</em>, <em>min=None</em>, <em>return_key=False</em>, <em>show_record_id=False</em>, <em>snapshot=False</em>, <em>comment=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find" title="Permalink to this definition">¶</a></dt> <dd><p>Query the database.</p> <p>The <cite>filter</cite> argument is a prototype document that all results must match. For example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s2">"hello"</span><span class="p">:</span> <span class="s2">"world"</span><span class="p">})</span> </pre></div> </div> <p>only matches documents that have a key “hello” with value “world”. Matches can have other keys <em>in addition</em> to “hello”. The <cite>projection</cite> argument is used to specify a subset of fields that should be included in the result documents. By limiting results to a certain subset of fields you can cut down on network traffic and decoding time.</p> <p>Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if any of the arguments are of improper type. Returns an instance of <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> corresponding to this query.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</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>filter</cite> (optional): a SON object specifying elements which must be present for a document to be included in the result set</li> <li><cite>projection</cite> (optional): a list of field names that should be returned in the result set or a dict specifying the fields to include or exclude. If <cite>projection</cite> is a list “_id” will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id’: False}).</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>skip</cite> (optional): the number of documents to omit (from the start of the result set) when returning the results</li> <li><cite>limit</cite> (optional): the maximum number of results to return</li> <li><cite>no_cursor_timeout</cite> (optional): if False (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to True, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with no_cursor_timeout turned on are properly closed.</li> <li><cite>cursor_type</cite> (optional): the type of cursor to return. The valid options are defined by <a class="reference internal" href="cursor.html#pymongo.cursor.CursorType" title="pymongo.cursor.CursorType"><code class="xref py py-class docutils literal notranslate"><span class="pre">CursorType</span></code></a>:<ul> <li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.NON_TAILABLE" title="pymongo.cursor.CursorType.NON_TAILABLE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">NON_TAILABLE</span></code></a> - the result of this find call will return a standard cursor over the result set.</li> <li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.TAILABLE" title="pymongo.cursor.CursorType.TAILABLE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TAILABLE</span></code></a> - the result of this find call will be a tailable cursor - tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the cursor location marks the final document position. If more data is received iteration of the cursor will continue from the last document received. For details, see the <a class="reference external" href="http://www.mongodb.org/display/DOCS/Tailable+Cursors">tailable cursor documentation</a>.</li> <li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.TAILABLE_AWAIT" title="pymongo.cursor.CursorType.TAILABLE_AWAIT"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TAILABLE_AWAIT</span></code></a> - the result of this find call will be a tailable cursor with the await flag set. The server will wait for a few seconds after returning the full result set so that it can capture and return additional data added during the query.</li> <li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.EXHAUST" title="pymongo.cursor.CursorType.EXHAUST"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EXHAUST</span></code></a> - the result of this find call will be an exhaust cursor. MongoDB will stream batched results to the client without waiting for the client to request each batch, reducing latency. See notes on compatibility below.</li> </ul> </li> <li><cite>sort</cite> (optional): a list of (key, direction) pairs specifying the sort order for this query. See <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.sort" title="pymongo.cursor.Cursor.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code></a> for details.</li> <li><cite>allow_partial_results</cite> (optional): if True, mongos will return partial results if some shards are down instead of returning an error.</li> <li><cite>oplog_replay</cite> (optional): If True, set the oplogReplay query flag.</li> <li><cite>batch_size</cite> (optional): Limits the number of documents returned in a single batch.</li> <li><cite>manipulate</cite> (optional): <strong>DEPRECATED</strong> - If True (the default), apply any outgoing SON manipulators before returning.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>return_key</cite> (optional): If True, return only the index keys in each document.</li> <li><cite>show_record_id</cite> (optional): If True, adds a field <code class="docutils literal notranslate"><span class="pre">$recordId</span></code> in each document with the storage engine’s internal record identifier.</li> <li><cite>snapshot</cite> (optional): <strong>DEPRECATED</strong> - If True, prevents the cursor from returning a document more than once because of an intervening write operation.</li> <li><cite>hint</cite> (optional): An index, in the same format as passed to <a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_index()</span></code></a> (e.g. <code class="docutils literal notranslate"><span class="pre">[('field',</span> <span class="pre">ASCENDING)]</span></code>). Pass this as an alternative to calling <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.hint" title="pymongo.cursor.Cursor.hint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hint()</span></code></a> on the cursor to tell Mongo the proper index to use for the query.</li> <li><cite>max_time_ms</cite> (optional): Specifies a time limit for a query operation. If the specified time is exceeded, the operation will be aborted and <a class="reference internal" href="errors.html#pymongo.errors.ExecutionTimeout" title="pymongo.errors.ExecutionTimeout"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExecutionTimeout</span></code></a> is raised. Pass this as an alternative to calling <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.max_time_ms" title="pymongo.cursor.Cursor.max_time_ms"><code class="xref py py-meth docutils literal notranslate"><span class="pre">max_time_ms()</span></code></a> on the cursor.</li> <li><cite>max_scan</cite> (optional): <strong>DEPRECATED</strong> - The maximum number of documents to scan. Pass this as an alternative to calling <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.max_scan" title="pymongo.cursor.Cursor.max_scan"><code class="xref py py-meth docutils literal notranslate"><span class="pre">max_scan()</span></code></a> on the cursor.</li> <li><cite>min</cite> (optional): A list of field, limit pairs specifying the inclusive lower bound for all keys of a specific index in order. Pass this as an alternative to calling <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.min" title="pymongo.cursor.Cursor.min"><code class="xref py py-meth docutils literal notranslate"><span class="pre">min()</span></code></a> on the cursor.</li> <li><cite>max</cite> (optional): A list of field, limit pairs specifying the exclusive upper bound for all keys of a specific index in order. Pass this as an alternative to calling <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.max" title="pymongo.cursor.Cursor.max"><code class="xref py py-meth docutils literal notranslate"><span class="pre">max()</span></code></a> on the cursor.</li> <li><cite>comment</cite> (optional): A string or document. Pass this as an alternative to calling <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.comment" title="pymongo.cursor.Cursor.comment"><code class="xref py py-meth docutils literal notranslate"><span class="pre">comment()</span></code></a> on the cursor.</li> <li><cite>modifiers</cite> (optional): <strong>DEPRECATED</strong> - A dict specifying additional MongoDB query modifiers. Use the keyword arguments listed above instead.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>There are a number of caveats to using <a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.EXHAUST" title="pymongo.cursor.CursorType.EXHAUST"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EXHAUST</span></code></a> as cursor_type:</p> <ul class="last simple"> <li>The <cite>limit</cite> option can not be used with an exhaust cursor.</li> <li>Exhaust cursors are not supported by mongos and can not be used with a sharded cluster.</li> <li>A <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> instance created with the <a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.EXHAUST" title="pymongo.cursor.CursorType.EXHAUST"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EXHAUST</span></code></a> cursor_type requires an exclusive <code class="xref py py-class docutils literal notranslate"><span class="pre">socket</span></code> connection to MongoDB. If the <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cursor</span></code></a> is discarded without being completely iterated the underlying <code class="xref py py-class docutils literal notranslate"><span class="pre">socket</span></code> connection will be closed and discarded without being returned to the connection pool.</li> </ul> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Deprecated the <cite>snapshot</cite> option, which is deprecated in MongoDB 3.6 and removed in MongoDB 4.0. Deprecated the <cite>max_scan</cite> option. Support for this option is deprecated in MongoDB 4.0. Use <cite>max_time_ms</cite> instead to limit server side execution time.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Added the options <cite>return_key</cite>, <cite>show_record_id</cite>, <cite>snapshot</cite>, <cite>hint</cite>, <cite>max_time_ms</cite>, <cite>max_scan</cite>, <cite>min</cite>, <cite>max</cite>, and <cite>comment</cite>. Deprecated the option <cite>modifiers</cite>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Changed the parameter names <cite>spec</cite>, <cite>fields</cite>, <cite>timeout</cite>, and <cite>partial</cite> to <cite>filter</cite>, <cite>projection</cite>, <cite>no_cursor_timeout</cite>, and <cite>allow_partial_results</cite> respectively. Added the <cite>cursor_type</cite>, <cite>oplog_replay</cite>, and <cite>modifiers</cite> options. Removed the <cite>network_timeout</cite>, <cite>read_preference</cite>, <cite>tag_sets</cite>, <cite>secondary_acceptable_latency_ms</cite>, <cite>max_scan</cite>, <cite>snapshot</cite>, <cite>tailable</cite>, <cite>await_data</cite>, <cite>exhaust</cite>, <cite>as_class</cite>, and slave_okay parameters. Removed <cite>compile_re</cite> option: PyMongo now always represents BSON regular expressions as <a class="reference internal" href="../bson/regex.html#bson.regex.Regex" title="bson.regex.Regex"><code class="xref py py-class docutils literal notranslate"><span class="pre">Regex</span></code></a> objects. Use <a class="reference internal" href="../bson/regex.html#bson.regex.Regex.try_compile" title="bson.regex.Regex.try_compile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">try_compile()</span></code></a> to attempt to convert from a BSON regular expression to a Python regular expression object. Soft deprecated the <cite>manipulate</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.7: </span>Added <cite>compile_re</cite> option. If set to False, PyMongo represented BSON regular expressions as <a class="reference internal" href="../bson/regex.html#bson.regex.Regex" title="bson.regex.Regex"><code class="xref py py-class docutils literal notranslate"><span class="pre">Regex</span></code></a> objects instead of attempting to compile BSON regular expressions as Python native regular expressions, thus preventing errors for some incompatible patterns, see <a class="reference external" href="https://jira.mongodb.org/browse/PYTHON-500">PYTHON-500</a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.3: </span>The <cite>tag_sets</cite> and <cite>secondary_acceptable_latency_ms</cite> parameters.</p> </div> <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/find" name="pymongo.collection.Collection.find"><em>find</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find_raw_batches"> <code class="descname">find_raw_batches</code><span class="sig-paren">(</span><em>filter=None</em>, <em>projection=None</em>, <em>skip=0</em>, <em>limit=0</em>, <em>no_cursor_timeout=False</em>, <em>cursor_type=CursorType.NON_TAILABLE</em>, <em>sort=None</em>, <em>allow_partial_results=False</em>, <em>oplog_replay=False</em>, <em>modifiers=None</em>, <em>batch_size=0</em>, <em>manipulate=True</em>, <em>collation=None</em>, <em>hint=None</em>, <em>max_scan=None</em>, <em>max_time_ms=None</em>, <em>max=None</em>, <em>min=None</em>, <em>return_key=False</em>, <em>show_record_id=False</em>, <em>snapshot=False</em>, <em>comment=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_raw_batches" title="Permalink to this definition">¶</a></dt> <dd><p>Query the database and retrieve batches of raw BSON.</p> <p>Similar to the <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a> method but returns a <a class="reference internal" href="cursor.html#pymongo.cursor.RawBatchCursor" title="pymongo.cursor.RawBatchCursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">RawBatchCursor</span></code></a>.</p> <p>This example demonstrates how to work with raw batches, but in practice raw batches should be passed to an external library that can decode BSON into another data type, rather than used with PyMongo’s <a class="reference internal" href="../bson/index.html#module-bson" title="bson: BSON (Binary JSON) Encoding and Decoding"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bson</span></code></a> module.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">bson</span> <span class="gp">>>> </span><span class="n">cursor</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_raw_batches</span><span class="p">()</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">batch</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">bson</span><span class="o">.</span><span class="n">decode_all</span><span class="p">(</span><span class="n">batch</span><span class="p">))</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">find_raw_batches does not support sessions.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find_one"> <code class="descname">find_one</code><span class="sig-paren">(</span><em>filter=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one" title="Permalink to this definition">¶</a></dt> <dd><p>Get a single document from the database.</p> <p>All arguments to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a> are also valid arguments for <a class="reference internal" href="#pymongo.collection.Collection.find_one" title="pymongo.collection.Collection.find_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one()</span></code></a>, although any <cite>limit</cite> argument will be ignored. Returns a single document, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no matching document is found.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.find_one" title="pymongo.collection.Collection.find_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</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"> <li><p class="first"><cite>filter</cite> (optional): a dictionary specifying the query to be performed OR any other type to be used as the value for a query for <code class="docutils literal notranslate"><span class="pre">"_id"</span></code>.</p> </li> <li><p class="first"><cite>*args</cite> (optional): any additional positional arguments are the same as the arguments to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a>.</p> </li> <li><p class="first"><cite>**kwargs</cite> (optional): any additional keyword arguments are the same as the arguments to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a>.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">(</span><span class="n">max_time_ms</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span> </pre></div> </div> </li> </ul> </td> </tr> </tbody> </table> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find_one_and_delete"> <code class="descname">find_one_and_delete</code><span class="sig-paren">(</span><em>filter</em>, <em>projection=None</em>, <em>sort=None</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one_and_delete" title="Permalink to this definition">¶</a></dt> <dd><p>Finds a single document and deletes it, returning the document.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">2</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_delete</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">{u'x': 1, u'_id': ObjectId('54f4e12bfba5220aa4d6dee8')}</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count_documents</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">1</span> </pre></div> </div> <p>If multiple documents match <em>filter</em>, a <em>sort</em> can be applied.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': 0}</span> <span class="go">{u'x': 1, u'_id': 1}</span> <span class="go">{u'x': 1, u'_id': 2}</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_delete</span><span class="p">(</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">sort</span><span class="o">=</span><span class="p">[(</span><span class="s1">'_id'</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">)])</span> <span class="go">{u'x': 1, u'_id': 2}</span> </pre></div> </div> <p>The <em>projection</em> option can be used to limit the fields returned.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_delete</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="kc">False</span><span class="p">})</span> <span class="go">{u'x': 1}</span> </pre></div> </div> <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>filter</cite>: A query that matches the document to delete.</li> <li><cite>projection</cite> (optional): a list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If <cite>projection</cite> is a list “_id” will always be returned. Use a mapping to exclude fields from the result (e.g. projection={‘_id’: False}).</li> <li><cite>sort</cite> (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is deleted.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Respects write concern.</p> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Starting in PyMongo 3.2, this command uses the <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteConcern</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> when connected to MongoDB >= 3.2. Note that using an elevated write concern with this command may be slower compared to using the default write concern.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find_one_and_replace"> <code class="descname">find_one_and_replace</code><span class="sig-paren">(</span><em>filter</em>, <em>replacement</em>, <em>projection=None</em>, <em>sort=None</em>, <em>return_document=ReturnDocument.BEFORE</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one_and_replace" title="Permalink to this definition">¶</a></dt> <dd><p>Finds a single document and replaces it, returning either the original or the replaced document.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_replace" title="pymongo.collection.Collection.find_one_and_replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_replace()</span></code></a> method differs from <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_update()</span></code></a> by replacing the document matched by <em>filter</em>, rather than modifying the existing document.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'x': 1, u'_id': 0}</span> <span class="go">{u'x': 1, u'_id': 1}</span> <span class="go">{u'x': 1, u'_id': 2}</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_replace</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'y'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span> <span class="go">{u'x': 1, u'_id': 0}</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'y': 1, u'_id': 0}</span> <span class="go">{u'x': 1, u'_id': 1}</span> <span class="go">{u'x': 1, u'_id': 2}</span> </pre></div> </div> <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>filter</cite>: A query that matches the document to replace.</li> <li><cite>replacement</cite>: The replacement document.</li> <li><cite>projection</cite> (optional): A list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If <cite>projection</cite> is a list “_id” will always be returned. Use a mapping to exclude fields from the result (e.g. projection={‘_id’: False}).</li> <li><cite>sort</cite> (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is replaced.</li> <li><cite>upsert</cite> (optional): When <code class="docutils literal notranslate"><span class="pre">True</span></code>, inserts a new document if no document matches the query. Defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>return_document</cite>: If <a class="reference internal" href="#pymongo.collection.ReturnDocument.BEFORE" title="pymongo.collection.ReturnDocument.BEFORE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ReturnDocument.BEFORE</span></code></a> (the default), returns the original document before it was replaced, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no document matches. If <a class="reference internal" href="#pymongo.collection.ReturnDocument.AFTER" title="pymongo.collection.ReturnDocument.AFTER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ReturnDocument.AFTER</span></code></a>, returns the replaced or inserted document.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Respects write concern.</p> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Starting in PyMongo 3.2, this command uses the <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteConcern</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> when connected to MongoDB >= 3.2. Note that using an elevated write concern with this command may be slower compared to using the default write concern.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find_one_and_update"> <code class="descname">find_one_and_update</code><span class="sig-paren">(</span><em>filter</em>, <em>update</em>, <em>projection=None</em>, <em>sort=None</em>, <em>return_document=ReturnDocument.BEFORE</em>, <em>array_filters=None</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one_and_update" title="Permalink to this definition">¶</a></dt> <dd><p>Finds a single document and updates it, returning either the original or the updated document.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="mi">665</span><span class="p">},</span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'count'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="s1">'$set'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'done'</span><span class="p">:</span> <span class="kc">True</span><span class="p">}})</span> <span class="go">{u'_id': 665, u'done': False, u'count': 25}}</span> </pre></div> </div> <p>By default <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_update()</span></code></a> returns the original version of the document before the update was applied. To return the updated version of the document instead, use the <em>return_document</em> option.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">ReturnDocument</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="s1">'userid'</span><span class="p">},</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}},</span> <span class="gp">... </span> <span class="n">return_document</span><span class="o">=</span><span class="n">ReturnDocument</span><span class="o">.</span><span class="n">AFTER</span><span class="p">)</span> <span class="go">{u'_id': u'userid', u'seq': 1}</span> </pre></div> </div> <p>You can limit the fields returned with the <em>projection</em> option.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="s1">'userid'</span><span class="p">},</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}},</span> <span class="gp">... </span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">'_id'</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span> <span class="gp">... </span> <span class="n">return_document</span><span class="o">=</span><span class="n">ReturnDocument</span><span class="o">.</span><span class="n">AFTER</span><span class="p">)</span> <span class="go">{u'seq': 2}</span> </pre></div> </div> <p>The <em>upsert</em> option can be used to create the document if it doesn’t already exist.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">delete_many</span><span class="p">({})</span><span class="o">.</span><span class="n">deleted_count</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="s1">'userid'</span><span class="p">},</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}},</span> <span class="gp">... </span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">'_id'</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span> <span class="gp">... </span> <span class="n">upsert</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="gp">... </span> <span class="n">return_document</span><span class="o">=</span><span class="n">ReturnDocument</span><span class="o">.</span><span class="n">AFTER</span><span class="p">)</span> <span class="go">{u'seq': 1}</span> </pre></div> </div> <p>If multiple documents match <em>filter</em>, a <em>sort</em> can be applied.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s1">'done'</span><span class="p">:</span> <span class="kc">True</span><span class="p">}):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span> <span class="gp">...</span> <span class="go">{u'_id': 665, u'done': True, u'result': {u'count': 26}}</span> <span class="go">{u'_id': 701, u'done': True, u'result': {u'count': 17}}</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'done'</span><span class="p">:</span> <span class="kc">True</span><span class="p">},</span> <span class="gp">... </span> <span class="p">{</span><span class="s1">'$set'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'final'</span><span class="p">:</span> <span class="kc">True</span><span class="p">}},</span> <span class="gp">... </span> <span class="n">sort</span><span class="o">=</span><span class="p">[(</span><span class="s1">'_id'</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">)])</span> <span class="go">{u'_id': 701, u'done': True, u'result': {u'count': 17}}</span> </pre></div> </div> <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>filter</cite>: A query that matches the document to update.</li> <li><cite>update</cite>: The update operations to apply.</li> <li><cite>projection</cite> (optional): A list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If <cite>projection</cite> is a list “_id” will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id’: False}).</li> <li><cite>sort</cite> (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is updated.</li> <li><cite>upsert</cite> (optional): When <code class="docutils literal notranslate"><span class="pre">True</span></code>, inserts a new document if no document matches the query. Defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> <li><cite>return_document</cite>: If <a class="reference internal" href="#pymongo.collection.ReturnDocument.BEFORE" title="pymongo.collection.ReturnDocument.BEFORE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ReturnDocument.BEFORE</span></code></a> (the default), returns the original document before it was updated, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no document matches. If <a class="reference internal" href="#pymongo.collection.ReturnDocument.AFTER" title="pymongo.collection.ReturnDocument.AFTER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ReturnDocument.AFTER</span></code></a>, returns the updated or inserted document.</li> <li><cite>array_filters</cite> (optional): A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added the <cite>array_filters</cite> and <cite>session</cite> options.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Respects write concern.</p> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Starting in PyMongo 3.2, this command uses the <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal notranslate"><span class="pre">WriteConcern</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> when connected to MongoDB >= 3.2. Note that using an elevated write concern with this command may be slower compared to using the default write concern.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.count_documents"> <code class="descname">count_documents</code><span class="sig-paren">(</span><em>filter</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.count_documents" title="Permalink to this definition">¶</a></dt> <dd><p>Count the number of documents in this collection.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.count_documents" title="pymongo.collection.Collection.count_documents"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count_documents()</span></code></a> method is supported in a transaction.</p> <p>All optional parameters should be passed as keyword arguments to this method. Valid options include:</p> <blockquote> <div><ul class="simple"> <li><cite>skip</cite> (int): The number of matching documents to skip before returning results.</li> <li><cite>limit</cite> (int): The maximum number of documents to count.</li> <li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow this operation to run, in milliseconds.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>hint</cite> (string or list of tuples): The index to use. Specify either the index name as a string or the index specification as a list of tuples (e.g. [(‘a’, pymongo.ASCENDING), (‘b’, pymongo.ASCENDING)]). This option is only supported on MongoDB 3.6 and above.</li> </ul> </div></blockquote> <p>The <a class="reference internal" href="#pymongo.collection.Collection.count_documents" title="pymongo.collection.Collection.count_documents"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count_documents()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>When migrating from <a class="reference internal" href="#pymongo.collection.Collection.count" title="pymongo.collection.Collection.count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count()</span></code></a> to <a class="reference internal" href="#pymongo.collection.Collection.count_documents" title="pymongo.collection.Collection.count_documents"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count_documents()</span></code></a> the following query operators must be replaced:</p> <table border="1" class="docutils"> <colgroup> <col width="26%" /> <col width="74%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operator</th> <th class="head">Replacement</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>$where</td> <td><a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/expr/">$expr</a></td> </tr> <tr class="row-odd"><td>$near</td> <td><a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/geoWithin/">$geoWithin</a> with <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/center/#op._S_center">$center</a></td> </tr> <tr class="row-even"><td>$nearSphere</td> <td><a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/geoWithin/">$geoWithin</a> with <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere">$centerSphere</a></td> </tr> </tbody> </table> <p class="last">$expr requires MongoDB 3.6+</p> </div> <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>filter</cite> (required): A query document that selects which documents to count in the collection. Can be an empty document to count all documents.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): See list of options above.</li> </ul> </td> </tr> </tbody> </table> <div class="versionadded"> <p><span class="versionmodified">New in version 3.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.estimated_document_count"> <code class="descname">estimated_document_count</code><span class="sig-paren">(</span><em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.estimated_document_count" title="Permalink to this definition">¶</a></dt> <dd><p>Get an estimate of the number of documents in this collection using collection metadata.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.estimated_document_count" title="pymongo.collection.Collection.estimated_document_count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">estimated_document_count()</span></code></a> method is <strong>not</strong> supported in a transaction.</p> <p>All optional parameters should be passed as keyword arguments to this method. Valid options include:</p> <blockquote> <div><ul class="simple"> <li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow this operation to run, in milliseconds.</li> </ul> </div></blockquote> <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>**kwargs</cite> (optional): See list of options above.</li> </ul> </td> </tr> </tbody> </table> <div class="versionadded"> <p><span class="versionmodified">New in version 3.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.distinct"> <code class="descname">distinct</code><span class="sig-paren">(</span><em>key</em>, <em>filter=None</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.distinct" title="Permalink to this definition">¶</a></dt> <dd><p>Get a list of distinct values for <cite>key</cite> among all documents in this collection.</p> <p>Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if <cite>key</cite> is not an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code> in python 3).</p> <p>All optional distinct parameters should be passed as keyword arguments to this method. Valid options include:</p> <blockquote> <div><ul class="simple"> <li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow the count command to run, in milliseconds.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> </ul> </div></blockquote> <p>The <a class="reference internal" href="#pymongo.collection.Collection.distinct" title="pymongo.collection.Collection.distinct"><code class="xref py py-meth docutils literal notranslate"><span class="pre">distinct()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</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>key</cite>: name of the field for which we want to get the distinct values</li> <li><cite>filter</cite> (optional): A query document that specifies the documents from which to retrieve the distinct values.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): See list of options above.</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.create_index"> <code class="descname">create_index</code><span class="sig-paren">(</span><em>keys</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.create_index" title="Permalink to this definition">¶</a></dt> <dd><p>Creates an index on this collection.</p> <p>Takes either a single key or a list of (key, direction) pairs. The key(s) must be an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code> in python 3), and the direction(s) must be one of (<a class="reference internal" href="#pymongo.ASCENDING" title="pymongo.ASCENDING"><code class="xref py py-data docutils literal notranslate"><span class="pre">ASCENDING</span></code></a>, <a class="reference internal" href="#pymongo.DESCENDING" title="pymongo.DESCENDING"><code class="xref py py-data docutils literal notranslate"><span class="pre">DESCENDING</span></code></a>, <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><code class="xref py py-data docutils literal notranslate"><span class="pre">GEO2D</span></code></a>, <a class="reference internal" href="#pymongo.GEOHAYSTACK" title="pymongo.GEOHAYSTACK"><code class="xref py py-data docutils literal notranslate"><span class="pre">GEOHAYSTACK</span></code></a>, <a class="reference internal" href="#pymongo.GEOSPHERE" title="pymongo.GEOSPHERE"><code class="xref py py-data docutils literal notranslate"><span class="pre">GEOSPHERE</span></code></a>, <a class="reference internal" href="#pymongo.HASHED" title="pymongo.HASHED"><code class="xref py py-data docutils literal notranslate"><span class="pre">HASHED</span></code></a>, <a class="reference internal" href="#pymongo.TEXT" title="pymongo.TEXT"><code class="xref py py-data docutils literal notranslate"><span class="pre">TEXT</span></code></a>).</p> <p>To create a single key ascending index on the key <code class="docutils literal notranslate"><span class="pre">'mike'</span></code> we just use a string argument:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">my_collection</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span><span class="s2">"mike"</span><span class="p">)</span> </pre></div> </div> <p>For a compound index on <code class="docutils literal notranslate"><span class="pre">'mike'</span></code> descending and <code class="docutils literal notranslate"><span class="pre">'eliot'</span></code> ascending we need to use a list of tuples:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">my_collection</span><span class="o">.</span><span class="n">create_index</span><span class="p">([(</span><span class="s2">"mike"</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">),</span> <span class="gp">... </span> <span class="p">(</span><span class="s2">"eliot"</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">ASCENDING</span><span class="p">)])</span> </pre></div> </div> <p>All optional index creation parameters should be passed as keyword arguments to this method. For example:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">my_collection</span><span class="o">.</span><span class="n">create_index</span><span class="p">([(</span><span class="s2">"mike"</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">)],</span> <span class="gp">... </span> <span class="n">background</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> </pre></div> </div> <p>Valid options include, but are not limited to:</p> <blockquote> <div><ul class="simple"> <li><cite>name</cite>: custom name to use for this index - if none is given, a name will be generated.</li> <li><cite>unique</cite>: if <code class="docutils literal notranslate"><span class="pre">True</span></code> creates a uniqueness constraint on the index.</li> <li><cite>background</cite>: if <code class="docutils literal notranslate"><span class="pre">True</span></code> this index should be created in the background.</li> <li><cite>sparse</cite>: if <code class="docutils literal notranslate"><span class="pre">True</span></code>, omit from the index any documents that lack the indexed field.</li> <li><cite>bucketSize</cite>: for use with geoHaystack indexes. Number of documents to group together within a certain proximity to a given longitude and latitude.</li> <li><cite>min</cite>: minimum value for keys in a <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><code class="xref py py-data docutils literal notranslate"><span class="pre">GEO2D</span></code></a> index.</li> <li><cite>max</cite>: maximum value for keys in a <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><code class="xref py py-data docutils literal notranslate"><span class="pre">GEO2D</span></code></a> index.</li> <li><cite>expireAfterSeconds</cite>: <int> Used to create an expiring (TTL) collection. MongoDB will automatically delete documents from this collection after <int> seconds. The indexed field must be a UTC datetime or the data will not expire.</li> <li><cite>partialFilterExpression</cite>: A document that specifies a filter for a partial index.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> </ul> </div></blockquote> <p>See the MongoDB documentation for a full list of supported options by server version.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last"><cite>dropDups</cite> is not supported by MongoDB 3.0 or newer. The option is silently ignored by the server and unique index builds using the option will fail if a duplicate value is detected.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>partialFilterExpression</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation when using MongoDB >= 3.4.</p> </div> <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>keys</cite>: a single key or a list of (key, direction) pairs specifying the index to create</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): any additional index creation options (see the above list) should be passed as keyword arguments</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter. Added support for passing maxTimeMS in kwargs.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4. Support the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added partialFilterExpression to support partial indexes.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Renamed <cite>key_or_list</cite> to <cite>keys</cite>. Removed the <cite>cache_for</cite> option. <a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_index()</span></code></a> no longer caches index names. Removed support for the drop_dups and bucket_size aliases.</p> </div> <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/indexes" name="pymongo.collection.Collection.create_index"><em>indexes</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.create_indexes"> <code class="descname">create_indexes</code><span class="sig-paren">(</span><em>indexes</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.create_indexes" title="Permalink to this definition">¶</a></dt> <dd><p>Create one or more indexes on this collection.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">IndexModel</span><span class="p">,</span> <span class="n">ASCENDING</span><span class="p">,</span> <span class="n">DESCENDING</span> <span class="gp">>>> </span><span class="n">index1</span> <span class="o">=</span> <span class="n">IndexModel</span><span class="p">([(</span><span class="s2">"hello"</span><span class="p">,</span> <span class="n">DESCENDING</span><span class="p">),</span> <span class="gp">... </span> <span class="p">(</span><span class="s2">"world"</span><span class="p">,</span> <span class="n">ASCENDING</span><span class="p">)],</span> <span class="n">name</span><span class="o">=</span><span class="s2">"hello_world"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">index2</span> <span class="o">=</span> <span class="n">IndexModel</span><span class="p">([(</span><span class="s2">"goodbye"</span><span class="p">,</span> <span class="n">DESCENDING</span><span class="p">)])</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">create_indexes</span><span class="p">([</span><span class="n">index1</span><span class="p">,</span> <span class="n">index2</span><span class="p">])</span> <span class="go">["hello_world", "goodbye_-1"]</span> </pre></div> </div> <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>indexes</cite>: A list of <a class="reference internal" href="operations.html#pymongo.operations.IndexModel" title="pymongo.operations.IndexModel"><code class="xref py py-class docutils literal notranslate"><span class="pre">IndexModel</span></code></a> instances.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): optional arguments to the createIndexes command (like maxTimeMS) can be passed as keyword arguments.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>create_indexes</cite> uses the <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/createIndexes/">createIndexes</a> command introduced in MongoDB <strong>2.6</strong> and cannot be used with earlier versions.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation when using MongoDB >= 3.4.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter. Added support for arbitrary keyword arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.drop_index"> <code class="descname">drop_index</code><span class="sig-paren">(</span><em>index_or_name</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.drop_index" title="Permalink to this definition">¶</a></dt> <dd><p>Drops the specified index on this collection.</p> <p>Can be used on non-existant collections or collections with no indexes. Raises OperationFailure on an error (e.g. trying to drop an index that does not exist). <cite>index_or_name</cite> can be either an index name (as returned by <cite>create_index</cite>), or an index specifier (as passed to <cite>create_index</cite>). An index specifier should be a list of (key, direction) pairs. Raises TypeError if index is not an instance of (str, unicode, list).</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">if a custom name was used on index creation (by passing the <cite>name</cite> parameter to <a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_index()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_index()</span></code></a>) the index <strong>must</strong> be dropped by name.</p> </div> <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>index_or_name</cite>: index (or name of index) to drop</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): optional arguments to the createIndexes command (like maxTimeMS) can be passed as keyword arguments.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation when using MongoDB >= 3.4.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter. Added support for arbitrary keyword arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.drop_indexes"> <code class="descname">drop_indexes</code><span class="sig-paren">(</span><em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.drop_indexes" title="Permalink to this definition">¶</a></dt> <dd><p>Drops all indexes on this collection.</p> <p>Can be used on non-existant collections or collections with no indexes. Raises OperationFailure on an error.</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>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): optional arguments to the createIndexes command (like maxTimeMS) can be passed as keyword arguments.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation when using MongoDB >= 3.4.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter. Added support for arbitrary keyword arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.reindex"> <code class="descname">reindex</code><span class="sig-paren">(</span><em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.reindex" title="Permalink to this definition">¶</a></dt> <dd><p>Rebuilds all indexes on this collection.</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>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): optional arguments to the reIndex command (like maxTimeMS) can be passed as keyword arguments.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">reindex blocks all other operations (indexes are built in the foreground) and will be slow for large collections.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter. Added support for arbitrary keyword arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>We no longer apply this collection’s write concern to this operation. MongoDB 3.4 silently ignored the write concern. MongoDB 3.6+ returns an error if we include the write concern.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.list_indexes"> <code class="descname">list_indexes</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.list_indexes" title="Permalink to this definition">¶</a></dt> <dd><p>Get a cursor over the index documents for this collection.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">list_indexes</span><span class="p">():</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">index</span><span class="p">)</span> <span class="gp">...</span> <span class="go">SON([(u'v', 1), (u'key', SON([(u'_id', 1)])),</span> <span class="go"> (u'name', u'_id_'), (u'ns', u'test.test')])</span> </pre></div> </div> <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 simple"> <li><cite>session</cite> (optional): a <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>.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">An instance of <a class="reference internal" href="command_cursor.html#pymongo.command_cursor.CommandCursor" title="pymongo.command_cursor.CommandCursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">CommandCursor</span></code></a>.</p> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.index_information"> <code class="descname">index_information</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.index_information" title="Permalink to this definition">¶</a></dt> <dd><p>Get information on this collection’s indexes.</p> <p>Returns a dictionary where the keys are index names (as returned by create_index()) and the values are dictionaries containing information about each index. The dictionary is guaranteed to contain at least a single key, <code class="docutils literal notranslate"><span class="pre">"key"</span></code> which is a list of (key, direction) pairs specifying the index (as passed to create_index()). It will also contain any other metadata about the indexes, except for the <code class="docutils literal notranslate"><span class="pre">"ns"</span></code> and <code class="docutils literal notranslate"><span class="pre">"name"</span></code> keys, which are cleaned. Example output might look like this:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span><span class="s2">"x"</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="go">u'x_1'</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">index_information</span><span class="p">()</span> <span class="go">{u'_id_': {u'key': [(u'_id', 1)]},</span> <span class="go"> u'x_1': {u'unique': True, u'key': [(u'x', 1)]}}</span> </pre></div> </div> <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>session</cite> (optional): a <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>.</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.drop"> <code class="descname">drop</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.drop" title="Permalink to this definition">¶</a></dt> <dd><p>Alias for <a class="reference internal" href="database.html#pymongo.database.Database.drop_collection" title="pymongo.database.Database.drop_collection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">drop_collection()</span></code></a>.</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>session</cite> (optional): a <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>.</li> </ul> </td> </tr> </tbody> </table> <p>The following two calls are equivalent:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">drop</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">drop_collection</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">)</span> </pre></div> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span><a class="reference internal" href="#pymongo.collection.Collection.drop" title="pymongo.collection.Collection.drop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">drop()</span></code></a> now respects this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>’s <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.rename"> <code class="descname">rename</code><span class="sig-paren">(</span><em>new_name</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.rename" title="Permalink to this definition">¶</a></dt> <dd><p>Rename this collection.</p> <p>If operating in auth mode, client must be authorized as an admin to perform this operation. Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if <cite>new_name</cite> is not an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code> in python 3). Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidName</span></code></a> if <cite>new_name</cite> is not a valid collection name.</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>new_name</cite>: new name for this collection</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): additional arguments to the rename command may be passed as keyword arguments to this helper method (i.e. <code class="docutils literal notranslate"><span class="pre">dropTarget=True</span></code>)</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation when using MongoDB >= 3.4.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.options"> <code class="descname">options</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.options" title="Permalink to this definition">¶</a></dt> <dd><p>Get the options set on this collection.</p> <p>Returns a dictionary of options and their values - see <a class="reference internal" href="database.html#pymongo.database.Database.create_collection" title="pymongo.database.Database.create_collection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_collection()</span></code></a> for more information on the possible options. Returns an empty dictionary if the collection has not been created yet.</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>session</cite> (optional): a <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>.</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.map_reduce"> <code class="descname">map_reduce</code><span class="sig-paren">(</span><em>map</em>, <em>reduce</em>, <em>out</em>, <em>full_response=False</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.map_reduce" title="Permalink to this definition">¶</a></dt> <dd><p>Perform a map/reduce operation on this collection.</p> <p>If <cite>full_response</cite> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (default) returns a <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> instance containing the results of the operation. Otherwise, returns the full response from the server to the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a>.</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"> <li><p class="first"><cite>map</cite>: map function (as a JavaScript string)</p> </li> <li><p class="first"><cite>reduce</cite>: reduce function (as a JavaScript string)</p> </li> <li><p class="first"><cite>out</cite>: output collection name or <cite>out object</cite> (dict). See the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a> documentation for available options. Note: <cite>out</cite> options are order sensitive. <a class="reference internal" href="../bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal notranslate"><span class="pre">SON</span></code></a> can be used to specify multiple options. e.g. SON([(‘replace’, <collection name>), (‘db’, <database name>)])</p> </li> <li><p class="first"><cite>full_response</cite> (optional): if <code class="docutils literal notranslate"><span class="pre">True</span></code>, return full response to this command - otherwise just return the result collection</p> </li> <li><p class="first"><cite>session</cite> (optional): a <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>.</p> </li> <li><p class="first"><cite>**kwargs</cite> (optional): additional arguments to the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a> may be passed as keyword arguments to this helper method, e.g.:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">map_reduce</span><span class="p">(</span><span class="nb">map</span><span class="p">,</span> <span class="n">reduce</span><span class="p">,</span> <span class="s2">"myresults"</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> </pre></div> </div> </li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.map_reduce" title="pymongo.collection.Collection.map_reduce"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map_reduce()</span></code></a> method does <strong>not</strong> obey the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>. To run mapReduce on a secondary use the <a class="reference internal" href="#pymongo.collection.Collection.inline_map_reduce" title="pymongo.collection.Collection.inline_map_reduce"><code class="xref py py-meth docutils literal notranslate"><span class="pre">inline_map_reduce()</span></code></a> method instead.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this collection is automatically applied to this operation (if the output is not inline) when using MongoDB >= 3.4.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference internal" href="../../examples/aggregation.html"><span class="doc">Aggregation Examples</span></a></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated arguments: merge_output and reduce_output</p> </div> <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/mapreduce" name="pymongo.collection.Collection.map_reduce"><em>mapreduce</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.inline_map_reduce"> <code class="descname">inline_map_reduce</code><span class="sig-paren">(</span><em>map</em>, <em>reduce</em>, <em>full_response=False</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.inline_map_reduce" title="Permalink to this definition">¶</a></dt> <dd><p>Perform an inline map/reduce operation on this collection.</p> <p>Perform the map/reduce operation on the server in RAM. A result collection is not created. The result set is returned as a list of documents.</p> <p>If <cite>full_response</cite> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (default) returns the result documents in a list. Otherwise, returns the full response from the server to the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a>.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.inline_map_reduce" title="pymongo.collection.Collection.inline_map_reduce"><code class="xref py py-meth docutils literal notranslate"><span class="pre">inline_map_reduce()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</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"> <li><p class="first"><cite>map</cite>: map function (as a JavaScript string)</p> </li> <li><p class="first"><cite>reduce</cite>: reduce function (as a JavaScript string)</p> </li> <li><p class="first"><cite>full_response</cite> (optional): if <code class="docutils literal notranslate"><span class="pre">True</span></code>, return full response to this command - otherwise just return the result collection</p> </li> <li><p class="first"><cite>session</cite> (optional): a <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>.</p> </li> <li><p class="first"><cite>**kwargs</cite> (optional): additional arguments to the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a> may be passed as keyword arguments to this helper method, e.g.:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">inline_map_reduce</span><span class="p">(</span><span class="nb">map</span><span class="p">,</span> <span class="n">reduce</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> </pre></div> </div> </li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.parallel_scan"> <code class="descname">parallel_scan</code><span class="sig-paren">(</span><em>num_cursors</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.parallel_scan" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Scan this entire collection in parallel.</p> <p>Returns a list of up to <code class="docutils literal notranslate"><span class="pre">num_cursors</span></code> cursors that can be iterated concurrently. As long as the collection is not modified during scanning, each document appears once in one of the cursors result sets.</p> <p>For example, to process each document in a collection using some thread-safe <code class="docutils literal notranslate"><span class="pre">process_document()</span></code> function:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">process_cursor</span><span class="p">(</span><span class="n">cursor</span><span class="p">):</span> <span class="gp">... </span> <span class="k">for</span> <span class="n">document</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span> <span class="gp">... </span> <span class="c1"># Some thread-safe processing function:</span> <span class="gp">... </span> <span class="n">process_document</span><span class="p">(</span><span class="n">document</span><span class="p">)</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c1"># Get up to 4 cursors.</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">cursors</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">parallel_scan</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">threads</span> <span class="o">=</span> <span class="p">[</span> <span class="gp">... </span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">process_cursor</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">cursor</span><span class="p">,))</span> <span class="gp">... </span> <span class="k">for</span> <span class="n">cursor</span> <span class="ow">in</span> <span class="n">cursors</span><span class="p">]</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">thread</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span> <span class="gp">... </span> <span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">thread</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span> <span class="gp">... </span> <span class="n">thread</span><span class="o">.</span><span class="n">join</span><span class="p">()</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="c1"># All documents have now been processed.</span> </pre></div> </div> <p>The <a class="reference internal" href="#pymongo.collection.Collection.parallel_scan" title="pymongo.collection.Collection.parallel_scan"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parallel_scan()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</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>num_cursors</cite>: the number of cursors to return</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite>: additional options for the parallelCollectionScan command can be passed as keyword arguments.</li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Requires server version <strong>>= 2.5.5</strong>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Deprecated.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added back support for arbitrary keyword arguments. MongoDB 3.4 adds support for maxTimeMS as an option to the parallelCollectionScan command.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Removed support for arbitrary keyword arguments, since the parallelCollectionScan command has no optional arguments.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.initialize_unordered_bulk_op"> <code class="descname">initialize_unordered_bulk_op</code><span class="sig-paren">(</span><em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.initialize_unordered_bulk_op" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong> - Initialize an unordered batch of write operations.</p> <p>Operations will be performed on the server in arbitrary order, possibly in parallel. All operations will be attempted.</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>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> </ul> </td> </tr> </tbody> </table> <p>Returns a <a class="reference internal" href="bulk.html#pymongo.bulk.BulkOperationBuilder" title="pymongo.bulk.BulkOperationBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">BulkOperationBuilder</span></code></a> instance.</p> <p>See <a class="reference internal" href="../../examples/bulk.html#unordered-bulk"><span class="std std-ref">Unordered Bulk Write Operations</span></a> for examples.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated. Use <a class="reference internal" href="#pymongo.collection.Collection.bulk_write" title="pymongo.collection.Collection.bulk_write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bulk_write()</span></code></a> instead.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.initialize_ordered_bulk_op"> <code class="descname">initialize_ordered_bulk_op</code><span class="sig-paren">(</span><em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.initialize_ordered_bulk_op" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong> - Initialize an ordered batch of write operations.</p> <p>Operations will be performed on the server serially, in the order provided. If an error occurs all remaining operations are aborted.</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>bypass_document_validation</cite>: (optional) If <code class="docutils literal notranslate"><span class="pre">True</span></code>, allows the write to opt-out of document level validation. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</li> </ul> </td> </tr> </tbody> </table> <p>Returns a <a class="reference internal" href="bulk.html#pymongo.bulk.BulkOperationBuilder" title="pymongo.bulk.BulkOperationBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">BulkOperationBuilder</span></code></a> instance.</p> <p>See <a class="reference internal" href="../../examples/bulk.html#ordered-bulk"><span class="std std-ref">Ordered Bulk Write Operations</span></a> for examples.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><cite>bypass_document_validation</cite> requires server version <strong>>= 3.2</strong></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated. Use <a class="reference internal" href="#pymongo.collection.Collection.bulk_write" title="pymongo.collection.Collection.bulk_write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bulk_write()</span></code></a> instead.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.7.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.group"> <code class="descname">group</code><span class="sig-paren">(</span><em>key</em>, <em>condition</em>, <em>initial</em>, <em>reduce</em>, <em>finalize=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.group" title="Permalink to this definition">¶</a></dt> <dd><p>Perform a query similar to an SQL <em>group by</em> operation.</p> <p><strong>DEPRECATED</strong> - The group command was deprecated in MongoDB 3.4. The <a class="reference internal" href="#pymongo.collection.Collection.group" title="pymongo.collection.Collection.group"><code class="xref py py-meth docutils literal notranslate"><span class="pre">group()</span></code></a> method is deprecated and will be removed in PyMongo 4.0. Use <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">aggregate()</span></code></a> with the <cite>$group</cite> stage or <a class="reference internal" href="#pymongo.collection.Collection.map_reduce" title="pymongo.collection.Collection.map_reduce"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map_reduce()</span></code></a> instead.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated the group method.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: command</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.count"> <code class="descname">count</code><span class="sig-paren">(</span><em>filter=None</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.count" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong> - Get the number of documents in this collection.</p> <p>The <a class="reference internal" href="#pymongo.collection.Collection.count" title="pymongo.collection.Collection.count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count()</span></code></a> method is deprecated and <strong>not</strong> supported in a transaction. Please use <a class="reference internal" href="#pymongo.collection.Collection.count_documents" title="pymongo.collection.Collection.count_documents"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count_documents()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.estimated_document_count" title="pymongo.collection.Collection.estimated_document_count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">estimated_document_count()</span></code></a> instead.</p> <p>All optional count parameters should be passed as keyword arguments to this method. Valid options include:</p> <blockquote> <div><ul class="simple"> <li><cite>skip</cite> (int): The number of matching documents to skip before returning results.</li> <li><cite>limit</cite> (int): The maximum number of documents to count.</li> <li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow the count command to run, in milliseconds.</li> <li><cite>collation</cite> (optional): An instance of <a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collation</span></code></a>. This option is only supported on MongoDB 3.4 and above.</li> <li><cite>hint</cite> (string or list of tuples): The index to use. Specify either the index name as a string or the index specification as a list of tuples (e.g. [(‘a’, pymongo.ASCENDING), (‘b’, pymongo.ASCENDING)]).</li> </ul> </div></blockquote> <p>The <a class="reference internal" href="#pymongo.collection.Collection.count" title="pymongo.collection.Collection.count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>When migrating from <a class="reference internal" href="#pymongo.collection.Collection.count" title="pymongo.collection.Collection.count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count()</span></code></a> to <a class="reference internal" href="#pymongo.collection.Collection.count_documents" title="pymongo.collection.Collection.count_documents"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count_documents()</span></code></a> the following query operators must be replaced:</p> <table border="1" class="docutils"> <colgroup> <col width="26%" /> <col width="74%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Operator</th> <th class="head">Replacement</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>$where</td> <td><a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/expr/">$expr</a></td> </tr> <tr class="row-odd"><td>$near</td> <td><a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/geoWithin/">$geoWithin</a> with <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/center/#op._S_center">$center</a></td> </tr> <tr class="row-even"><td>$nearSphere</td> <td><a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/geoWithin/">$geoWithin</a> with <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere">$centerSphere</a></td> </tr> </tbody> </table> <p class="last">$expr requires MongoDB 3.6+</p> </div> <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>filter</cite> (optional): A query document that selects which documents to count in the collection.</li> <li><cite>session</cite> (optional): a <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>.</li> <li><cite>**kwargs</cite> (optional): See list of options above.</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Deprecated.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Added <code class="docutils literal notranslate"><span class="pre">session</span></code> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.insert"> <code class="descname">insert</code><span class="sig-paren">(</span><em>doc_or_docs</em>, <em>manipulate=True</em>, <em>check_keys=True</em>, <em>continue_on_error=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.insert" title="Permalink to this definition">¶</a></dt> <dd><p>Insert a document(s) into this collection.</p> <p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.insert_one" title="pymongo.collection.Collection.insert_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert_one()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.insert_many" title="pymongo.collection.Collection.insert_many"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert_many()</span></code></a> instead.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal notranslate"><span class="pre">w=0</span></code> for unacknowledged write operations.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.save"> <code class="descname">save</code><span class="sig-paren">(</span><em>to_save</em>, <em>manipulate=True</em>, <em>check_keys=True</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.save" title="Permalink to this definition">¶</a></dt> <dd><p>Save a document in this collection.</p> <p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.insert_one" title="pymongo.collection.Collection.insert_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert_one()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.replace_one" title="pymongo.collection.Collection.replace_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">replace_one()</span></code></a> instead.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal notranslate"><span class="pre">w=0</span></code> for unacknowledged write operations.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.update"> <code class="descname">update</code><span class="sig-paren">(</span><em>spec</em>, <em>document</em>, <em>upsert=False</em>, <em>manipulate=False</em>, <em>multi=False</em>, <em>check_keys=True</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.update" title="Permalink to this definition">¶</a></dt> <dd><p>Update a document(s) in this collection.</p> <p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.replace_one" title="pymongo.collection.Collection.replace_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">replace_one()</span></code></a>, <a class="reference internal" href="#pymongo.collection.Collection.update_one" title="pymongo.collection.Collection.update_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update_one()</span></code></a>, or <a class="reference internal" href="#pymongo.collection.Collection.update_many" title="pymongo.collection.Collection.update_many"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update_many()</span></code></a> instead.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal notranslate"><span class="pre">w=0</span></code> for unacknowledged write operations.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.remove"> <code class="descname">remove</code><span class="sig-paren">(</span><em>spec_or_id=None</em>, <em>multi=True</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.remove" title="Permalink to this definition">¶</a></dt> <dd><p>Remove a document(s) from this collection.</p> <p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.delete_one" title="pymongo.collection.Collection.delete_one"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_one()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.delete_many" title="pymongo.collection.Collection.delete_many"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_many()</span></code></a> instead.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal notranslate"><span class="pre">w=0</span></code> for unacknowledged write operations.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.find_and_modify"> <code class="descname">find_and_modify</code><span class="sig-paren">(</span><em>query={}</em>, <em>update=None</em>, <em>upsert=False</em>, <em>sort=None</em>, <em>full_response=False</em>, <em>manipulate=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_and_modify" title="Permalink to this definition">¶</a></dt> <dd><p>Update and return an object.</p> <p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_delete" title="pymongo.collection.Collection.find_one_and_delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_delete()</span></code></a>, <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_replace" title="pymongo.collection.Collection.find_one_and_replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_replace()</span></code></a>, or <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_one_and_update()</span></code></a> instead.</p> </dd></dl> <dl class="method"> <dt id="pymongo.collection.Collection.ensure_index"> <code class="descname">ensure_index</code><span class="sig-paren">(</span><em>key_or_list</em>, <em>cache_for=300</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.ensure_index" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong> - Ensures that an index exists on this collection.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span><strong>DEPRECATED</strong></p> </div> </dd></dl> </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="collation.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">collation</span></code> – Tools for working with collations.</a></p> <h4>Next topic</h4> <p class="topless"><a href="command_cursor.html" title="next chapter"><code class="docutils literal notranslate"><span class="pre">command_cursor</span></code> – Tools for iterating over MongoDB command 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/collection.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="command_cursor.html" title="command_cursor – Tools for iterating over MongoDB command results" >next</a> |</li> <li class="right" > <a href="collation.html" title="collation – Tools for working with collations." >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>