<!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>database – Database 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="driver_info" href="driver_info.html" /> <link rel="prev" title="cursor_manager – Managers to handle when cursors are killed after being closed" href="cursor_manager.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="driver_info.html" title="driver_info" accesskey="N">next</a> |</li> <li class="right" > <a href="cursor_manager.html" title="cursor_manager – Managers to handle when cursors are killed after being closed" 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.database"> <span id="database-database-level-operations"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">database</span></code> – Database level operations<a class="headerlink" href="#module-pymongo.database" title="Permalink to this headline">¶</a></h1> <p>Database level operations.</p> <dl class="data"> <dt id="pymongo.auth.MECHANISMS"> <code class="descclassname">pymongo.auth.</code><code class="descname">MECHANISMS</code><em class="property"> = frozenset(['MONGODB-X509', 'DEFAULT', 'PLAIN', 'GSSAPI', 'SCRAM-SHA-1', 'MONGODB-CR', 'SCRAM-SHA-256'])</em><a class="headerlink" href="#pymongo.auth.MECHANISMS" title="Permalink to this definition">¶</a></dt> <dd><p>The authentication mechanisms supported by PyMongo.</p> </dd></dl> <dl class="data"> <dt id="pymongo.OFF"> <code class="descclassname">pymongo.</code><code class="descname">OFF</code><em class="property"> = 0</em><a class="headerlink" href="#pymongo.OFF" title="Permalink to this definition">¶</a></dt> <dd><p>No database profiling.</p> </dd></dl> <dl class="data"> <dt id="pymongo.SLOW_ONLY"> <code class="descclassname">pymongo.</code><code class="descname">SLOW_ONLY</code><em class="property"> = 1</em><a class="headerlink" href="#pymongo.SLOW_ONLY" title="Permalink to this definition">¶</a></dt> <dd><p>Only profile slow operations.</p> </dd></dl> <dl class="data"> <dt id="pymongo.ALL"> <code class="descclassname">pymongo.</code><code class="descname">ALL</code><em class="property"> = 2</em><a class="headerlink" href="#pymongo.ALL" title="Permalink to this definition">¶</a></dt> <dd><p>Profile all operations.</p> </dd></dl> <dl class="class"> <dt id="pymongo.database.Database"> <em class="property">class </em><code class="descclassname">pymongo.database.</code><code class="descname">Database</code><span class="sig-paren">(</span><em>client</em>, <em>name</em>, <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.database.Database" title="Permalink to this definition">¶</a></dt> <dd><p>Get a database by client and name.</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 database 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>client</cite>: A <a class="reference internal" href="mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><code class="xref py py-class docutils literal notranslate"><span class="pre">MongoClient</span></code></a> instance.</li> <li><cite>name</cite>: The database name.</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) client.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) client.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) client.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) client.read_concern is used.</li> </ul> </td> </tr> </tbody> </table> <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/databases" name="pymongo.database.Database"><em>databases</em></a></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. <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> no longer returns an instance of <a class="reference internal" href="collection.html#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>db[‘__my_collection__’]</div></blockquote> <p>Not:</p> <blockquote> <div>db.__my_collection__</div></blockquote> </div> <dl class="describe"> <dt> <code class="descname">db[collection_name] || db.collection_name</code></dt> <dd><p>Get the <cite>collection_name</cite> <a class="reference internal" href="collection.html#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> of <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> <cite>db</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> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Use dictionary style access if <cite>collection_name</cite> is an attribute of the <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> class eg: db[<cite>collection_name</cite>].</p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.codec_options"> <code class="descname">codec_options</code><a class="headerlink" href="#pymongo.database.Database.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.database.Database.read_preference"> <code class="descname">read_preference</code><a class="headerlink" href="#pymongo.database.Database.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.database.Database.read_preference" title="pymongo.database.Database.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.database.Database.write_concern"> <code class="descname">write_concern</code><a class="headerlink" href="#pymongo.database.Database.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.database.Database.write_concern" title="pymongo.database.Database.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.database.Database.read_concern"> <code class="descname">read_concern</code><a class="headerlink" href="#pymongo.database.Database.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.database.Database.add_son_manipulator"> <code class="descname">add_son_manipulator</code><span class="sig-paren">(</span><em>manipulator</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.add_son_manipulator" title="Permalink to this definition">¶</a></dt> <dd><p>Add a new son manipulator to this database.</p> <p><strong>DEPRECATED</strong> - <cite>add_son_manipulator</cite> is deprecated.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.0: </span>Deprecated add_son_manipulator.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.add_user"> <code class="descname">add_user</code><span class="sig-paren">(</span><em>name</em>, <em>password=None</em>, <em>read_only=None</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.add_user" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Create user <cite>name</cite> with password <cite>password</cite>.</p> <p>Add a new user with permissions for this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Will change the password if user <cite>name</cite> already exists.</p> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>add_user is deprecated and will be removed in PyMongo 4.0. Starting with MongoDB 2.6 user management is handled with four database commands, <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/createUser/">createUser</a>, <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/usersInfo/">usersInfo</a>, <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/updateUser/">updateUser</a>, and <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/createUser/">dropUser</a>.</p> <p>To create a user:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s2">"createUser"</span><span class="p">,</span> <span class="s2">"admin"</span><span class="p">,</span> <span class="n">pwd</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="n">roles</span><span class="o">=</span><span class="p">[</span><span class="s2">"root"</span><span class="p">])</span> </pre></div> </div> <p>To create a read-only user:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s2">"createUser"</span><span class="p">,</span> <span class="s2">"user"</span><span class="p">,</span> <span class="n">pwd</span><span class="o">=</span><span class="s2">"password"</span><span class="p">,</span> <span class="n">roles</span><span class="o">=</span><span class="p">[</span><span class="s2">"read"</span><span class="p">])</span> </pre></div> </div> <p>To change a password:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s2">"updateUser"</span><span class="p">,</span> <span class="s2">"user"</span><span class="p">,</span> <span class="n">pwd</span><span class="o">=</span><span class="s2">"newpassword"</span><span class="p">)</span> </pre></div> </div> <p>Or change roles:</p> <div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s2">"updateUser"</span><span class="p">,</span> <span class="s2">"user"</span><span class="p">,</span> <span class="n">roles</span><span class="o">=</span><span class="p">[</span><span class="s2">"readWrite"</span><span class="p">])</span> </pre></div> </div> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Never create or modify users over an insecure network without the use of TLS. See <a class="reference internal" href="../../examples/tls.html"><span class="doc">TLS/SSL and PyMongo</span></a> for more information.</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>name</cite>: the name of the user to create</li> <li><cite>password</cite> (optional): the password of the user to create. Can not be used with the <code class="docutils literal notranslate"><span class="pre">userSource</span></code> argument.</li> <li><cite>read_only</cite> (optional): if <code class="docutils literal notranslate"><span class="pre">True</span></code> the user will be read only</li> <li><cite>**kwargs</cite> (optional): optional fields for the user document (e.g. <code class="docutils literal notranslate"><span class="pre">userSource</span></code>, <code class="docutils literal notranslate"><span class="pre">otherDBRoles</span></code>, or <code class="docutils literal notranslate"><span class="pre">roles</span></code>). See <a class="reference external" href="http://docs.mongodb.org/manual/reference/privilege-documents">http://docs.mongodb.org/manual/reference/privilege-documents</a> for more information.</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> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Added support for SCRAM-SHA-256 users with MongoDB 4.0 and later.</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. Deprecated add_user.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>Added kwargs support for optional fields introduced in MongoDB 2.4</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Added support for read only users</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.authenticate"> <code class="descname">authenticate</code><span class="sig-paren">(</span><em>name=None</em>, <em>password=None</em>, <em>source=None</em>, <em>mechanism='DEFAULT'</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.authenticate" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Authenticate to use this database.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Starting in MongoDB 3.6, calling <a class="reference internal" href="#pymongo.database.Database.authenticate" title="pymongo.database.Database.authenticate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">authenticate()</span></code></a> invalidates all existing cursors. It may also leave logical sessions open on the server for up to 30 minutes until they time out.</p> </div> <p>Authentication lasts for the life of the underlying client instance, or until <a class="reference internal" href="#pymongo.database.Database.logout" title="pymongo.database.Database.logout"><code class="xref py py-meth docutils literal notranslate"><span class="pre">logout()</span></code></a> is called.</p> <p>Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if (required) <cite>name</cite>, (optional) <cite>password</cite>, or (optional) <cite>source</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> <div class="admonition note"> <p class="first admonition-title">Note</p> <ul class="last simple"> <li>This method authenticates the current connection, and will also cause all new <code class="xref py py-class docutils literal notranslate"><span class="pre">socket</span></code> connections in the underlying client instance to be authenticated automatically.</li> <li>Authenticating more than once on the same database with different credentials is not supported. You must call <a class="reference internal" href="#pymongo.database.Database.logout" title="pymongo.database.Database.logout"><code class="xref py py-meth docutils literal notranslate"><span class="pre">logout()</span></code></a> before authenticating with new credentials.</li> <li>When sharing a client instance between multiple threads, all threads will share the authentication. If you need different authentication profiles for different purposes you must use distinct client instances.</li> </ul> </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>name</cite>: the name of the user to authenticate. Optional when <cite>mechanism</cite> is MONGODB-X509 and the MongoDB server version is >= 3.4.</li> <li><cite>password</cite> (optional): the password of the user to authenticate. Not used with GSSAPI or MONGODB-X509 authentication.</li> <li><cite>source</cite> (optional): the database to authenticate on. If not specified the current database is used.</li> <li><cite>mechanism</cite> (optional): See <a class="reference internal" href="#pymongo.auth.MECHANISMS" title="pymongo.auth.MECHANISMS"><code class="xref py py-data docutils literal notranslate"><span class="pre">MECHANISMS</span></code></a> for options. If no mechanism is specified, PyMongo automatically uses MONGODB-CR when connected to a pre-3.0 version of MongoDB, SCRAM-SHA-1 when connected to MongoDB 3.0 through 3.6, and negotiates the mechanism to use (SCRAM-SHA-1 or SCRAM-SHA-256) when connected to MongoDB 4.0+.</li> <li><cite>authMechanismProperties</cite> (optional): Used to specify authentication mechanism specific options. To specify the service name for GSSAPI authentication pass authMechanismProperties=’SERVICE_NAME:<service name>’</li> </ul> </td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Added support for SCRAM-SHA-256 with MongoDB 4.0 and later.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated. Authenticating multiple users conflicts with support for logical sessions in MongoDB 3.6. To authenticate as multiple users, create multiple instances of MongoClient.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.8: </span>Use SCRAM-SHA-1 with MongoDB 3.0 and later.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>Added the <cite>source</cite> and <cite>mechanism</cite> parameters. <a class="reference internal" href="#pymongo.database.Database.authenticate" title="pymongo.database.Database.authenticate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">authenticate()</span></code></a> now raises a subclass of <a class="reference internal" href="errors.html#pymongo.errors.PyMongoError" title="pymongo.errors.PyMongoError"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyMongoError</span></code></a> if authentication fails due to invalid credentials or configuration issues.</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/authenticate" name="pymongo.database.Database.authenticate"><em>authenticate</em></a></p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.client"> <code class="descname">client</code><a class="headerlink" href="#pymongo.database.Database.client" title="Permalink to this definition">¶</a></dt> <dd><p>The client instance for this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.collection_names"> <code class="descname">collection_names</code><span class="sig-paren">(</span><em>include_system_collections=True</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.collection_names" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Get a list of all the collection names in this database.</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>include_system_collections</cite> (optional): if <code class="docutils literal notranslate"><span class="pre">False</span></code> list will not include system collections (e.g <code class="docutils literal notranslate"><span class="pre">system.indexes</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> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.7: </span>Deprecated. Use <a class="reference internal" href="#pymongo.database.Database.list_collection_names" title="pymongo.database.Database.list_collection_names"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list_collection_names()</span></code></a> instead.</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.database.Database.command"> <code class="descname">command</code><span class="sig-paren">(</span><em>command</em>, <em>value=1</em>, <em>check=True</em>, <em>allowable_errors=None</em>, <em>read_preference=None</em>, <em>codec_options=CodecOptions(document_class=dict</em>, <em>tz_aware=False</em>, <em>uuid_representation=PYTHON_LEGACY</em>, <em>unicode_decode_error_handler='strict'</em>, <em>tzinfo=None)</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.command" title="Permalink to this definition">¶</a></dt> <dd><p>Issue a MongoDB command.</p> <p>Send command <cite>command</cite> to the database and return the response. If <cite>command</cite> is 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) then the command {<cite>command</cite>: <cite>value</cite>} will be sent. Otherwise, <cite>command</cite> must be an instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code> and will be sent as is.</p> <p>Any additional keyword arguments will be added to the final command document before it is sent.</p> <p>For example, a command like <code class="docutils literal notranslate"><span class="pre">{buildinfo:</span> <span class="pre">1}</span></code> can be sent using:</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">command</span><span class="p">(</span><span class="s2">"buildinfo"</span><span class="p">)</span> </pre></div> </div> <p>For a command where the value matters, like <code class="docutils literal notranslate"><span class="pre">{collstats:</span> <span class="pre">collection_name}</span></code> we can do:</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">command</span><span class="p">(</span><span class="s2">"collstats"</span><span class="p">,</span> <span class="n">collection_name</span><span class="p">)</span> </pre></div> </div> <p>For commands that take additional arguments we can use kwargs. So <code class="docutils literal notranslate"><span class="pre">{filemd5:</span> <span class="pre">object_id,</span> <span class="pre">root:</span> <span class="pre">file_root}</span></code> becomes:</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">command</span><span class="p">(</span><span class="s2">"filemd5"</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">root</span><span class="o">=</span><span class="n">file_root</span><span class="p">)</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"> <li><p class="first"><cite>command</cite>: document representing the command to be issued, or the name of the command (for simple commands only).</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">the order of keys in the <cite>command</cite> document is significant (the “verb” must come first), so commands which require multiple keys (e.g. <cite>findandmodify</cite>) should use an instance of <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> or a string and kwargs instead of a Python <cite>dict</cite>.</p> </div> </li> <li><p class="first"><cite>value</cite> (optional): value to use for the command verb when <cite>command</cite> is passed as a string</p> </li> <li><p class="first"><cite>check</cite> (optional): check the response for errors, raising <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><code class="xref py py-class docutils literal notranslate"><span class="pre">OperationFailure</span></code></a> if there are any</p> </li> <li><p class="first"><cite>allowable_errors</cite>: if <cite>check</cite> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, error messages in this list will be ignored by error-checking</p> </li> <li><p class="first"><cite>read_preference</cite> (optional): The read preference for this operation. 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. If the provided <cite>session</cite> is in a transaction, defaults to the read preference configured for the transaction. Otherwise, defaults to <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>.</p> </li> <li><p class="first"><cite>codec_options</cite>: A <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> instance.</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 keyword arguments will be added to the command document before it is sent</p> </li> </ul> </td> </tr> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><a class="reference internal" href="#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> does <strong>not</strong> obey this Database’s <a class="reference internal" href="#pymongo.database.Database.read_preference" title="pymongo.database.Database.read_preference"><code class="xref py py-attr docutils literal notranslate"><span class="pre">read_preference</span></code></a> or <a class="reference internal" href="#pymongo.database.Database.codec_options" title="pymongo.database.Database.codec_options"><code class="xref py py-attr docutils literal notranslate"><span class="pre">codec_options</span></code></a>. You must use the <cite>read_preference</cite> and <cite>codec_options</cite> parameters instead.</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.0: </span>Removed the <cite>as_class</cite>, <cite>fields</cite>, <cite>uuid_subtype</cite>, <cite>tag_sets</cite>, and <cite>secondary_acceptable_latency_ms</cite> option. 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. Added the <cite>codec_options</cite> parameter.</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="versionchanged"> <p><span class="versionmodified">Changed in version 2.3: </span>Added <cite>tag_sets</cite> and <cite>secondary_acceptable_latency_ms</cite> options.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Added support for <cite>as_class</cite> - the class you want to use for the resulting documents</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/commands" name="pymongo.database.Database.command"><em>commands</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.create_collection"> <code class="descname">create_collection</code><span class="sig-paren">(</span><em>name</em>, <em>codec_options=None</em>, <em>read_preference=None</em>, <em>write_concern=None</em>, <em>read_concern=None</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.create_collection" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <a class="reference internal" href="collection.html#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> in this database.</p> <p>Normally collection creation is automatic. This method should only be used to specify options on creation. <a class="reference internal" href="errors.html#pymongo.errors.CollectionInvalid" title="pymongo.errors.CollectionInvalid"><code class="xref py py-class docutils literal notranslate"><span class="pre">CollectionInvalid</span></code></a> will be raised if the collection already exists.</p> <p>Options should be passed as keyword arguments to this method. Supported options vary with MongoDB release. Some examples include:</p> <blockquote> <div><ul class="simple"> <li>“size”: desired initial size for the collection (in bytes). For capped collections this size is the max size of the collection.</li> <li>“capped”: if True, this is a capped collection</li> <li>“max”: maximum number of objects if capped (optional)</li> </ul> </div></blockquote> <p>See the MongoDB documentation for a full list of supported options by server version.</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>name</cite>: the name of the collection to create</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) the <a class="reference internal" href="#pymongo.database.Database.codec_options" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</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.database.Database.read_preference" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> 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) the <a class="reference internal" href="#pymongo.database.Database.write_concern" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</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.database.Database.read_concern" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> 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>.</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 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>Added the collation 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.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: options</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.current_op"> <code class="descname">current_op</code><span class="sig-paren">(</span><em>include_all=False</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.current_op" title="Permalink to this definition">¶</a></dt> <dd><p>Get information on operations currently running.</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>include_all</cite> (optional): if <code class="docutils literal notranslate"><span class="pre">True</span></code> also list currently idle operations in the result</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> </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.database.Database.dereference"> <code class="descname">dereference</code><span class="sig-paren">(</span><em>dbref</em>, <em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.dereference" title="Permalink to this definition">¶</a></dt> <dd><p>Dereference a <a class="reference internal" href="../bson/dbref.html#bson.dbref.DBRef" title="bson.dbref.DBRef"><code class="xref py py-class docutils literal notranslate"><span class="pre">DBRef</span></code></a>, getting the document it points to.</p> <p>Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code> if <cite>dbref</cite> is not an instance of <a class="reference internal" href="../bson/dbref.html#bson.dbref.DBRef" title="bson.dbref.DBRef"><code class="xref py py-class docutils literal notranslate"><span class="pre">DBRef</span></code></a>. Returns a document, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the reference does not point to a valid document. Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">ValueError</span></code> if <cite>dbref</cite> has a database specified that is different from the current database.</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>dbref</cite>: the reference</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 keyword arguments are the same as the arguments to <a class="reference internal" href="collection.html#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>.</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.database.Database.drop_collection"> <code class="descname">drop_collection</code><span class="sig-paren">(</span><em>name_or_collection</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.drop_collection" title="Permalink to this definition">¶</a></dt> <dd><p>Drop a 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>name_or_collection</cite>: the name of a collection to drop or the collection object itself</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> </tbody> </table> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#pymongo.database.Database.write_concern" title="pymongo.database.Database.write_concern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">write_concern</span></code></a> of this database 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 database’s write concern automatically to this operation when connected to MongoDB >= 3.4.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.error"> <code class="descname">error</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.error" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Get the error if one occurred on the last operation.</p> <p>This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern <code class="docutils literal notranslate"><span class="pre">w=1</span></code> and report their errors by default.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.8: </span>Deprecated.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.eval"> <code class="descname">eval</code><span class="sig-paren">(</span><em>code</em>, <em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.eval" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Evaluate a JavaScript expression in MongoDB.</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>code</cite>: string representation of JavaScript code to be evaluated</li> <li><cite>args</cite> (optional): additional positional arguments are passed to the <cite>code</cite> being evaluated</li> </ul> </td> </tr> </tbody> </table> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">the eval command is deprecated in MongoDB 3.0 and will be removed in a future server version.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.get_collection"> <code class="descname">get_collection</code><span class="sig-paren">(</span><em>name</em>, <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.database.Database.get_collection" title="Permalink to this definition">¶</a></dt> <dd><p>Get a <a class="reference internal" href="collection.html#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> with the given name and options.</p> <p>Useful for creating a <a class="reference internal" href="collection.html#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> with different codec options, read preference, and/or write concern from this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</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">read_preference</span> <span class="go">Primary()</span> <span class="gp">>>> </span><span class="n">coll1</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</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">db</span><span class="o">.</span><span class="n">get_collection</span><span class="p">(</span> <span class="gp">... </span> <span class="s1">'test'</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">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>name</cite>: The name of the collection - a string.</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) the <a class="reference internal" href="#pymongo.database.Database.codec_options" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</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.database.Database.read_preference" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</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.database.Database.write_concern" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</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.database.Database.read_concern" title="pymongo.database.Database.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.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> is used.</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.incoming_copying_manipulators"> <code class="descname">incoming_copying_manipulators</code><a class="headerlink" href="#pymongo.database.Database.incoming_copying_manipulators" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: All incoming SON copying manipulators.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.incoming_manipulators"> <code class="descname">incoming_manipulators</code><a class="headerlink" href="#pymongo.database.Database.incoming_manipulators" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: All incoming SON manipulators.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.last_status"> <code class="descname">last_status</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.last_status" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Get status information from the last operation.</p> <p>This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern <code class="docutils literal notranslate"><span class="pre">w=1</span></code> and report their errors by default.</p> <p>Returns a SON object with status information.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.8: </span>Deprecated.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.list_collection_names"> <code class="descname">list_collection_names</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.list_collection_names" title="Permalink to this definition">¶</a></dt> <dd><p>Get a list of all the collection names in this database.</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="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.list_collections"> <code class="descname">list_collections</code><span class="sig-paren">(</span><em>session=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.list_collections" title="Permalink to this definition">¶</a></dt> <dd><p>Get a cursor over the collectons of this database.</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 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 parameters of the <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/listCollections/">listCollections command</a> can be passed as keyword arguments to this method. The supported options differ by server version.</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="versionadded"> <p><span class="versionmodified">New in version 3.6.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.logout"> <code class="descname">logout</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.logout" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Deauthorize use of this database.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Starting in MongoDB 3.6, calling <a class="reference internal" href="#pymongo.database.Database.logout" title="pymongo.database.Database.logout"><code class="xref py py-meth docutils literal notranslate"><span class="pre">logout()</span></code></a> invalidates all existing cursors. It may also leave logical sessions open on the server for up to 30 minutes until they time out.</p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.name"> <code class="descname">name</code><a class="headerlink" href="#pymongo.database.Database.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</p> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.outgoing_copying_manipulators"> <code class="descname">outgoing_copying_manipulators</code><a class="headerlink" href="#pymongo.database.Database.outgoing_copying_manipulators" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: All outgoing SON copying manipulators.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.outgoing_manipulators"> <code class="descname">outgoing_manipulators</code><a class="headerlink" href="#pymongo.database.Database.outgoing_manipulators" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: All outgoing SON manipulators.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Deprecated.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.0.</span></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.previous_error"> <code class="descname">previous_error</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.previous_error" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Get the most recent error on this database.</p> <p>This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern <code class="docutils literal notranslate"><span class="pre">w=1</span></code> and report their errors by default.</p> <p>Only returns errors that have occurred since the last call to <a class="reference internal" href="#pymongo.database.Database.reset_error_history" title="pymongo.database.Database.reset_error_history"><code class="xref py py-meth docutils literal notranslate"><span class="pre">reset_error_history()</span></code></a>. Returns None if no such errors have occurred.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.8: </span>Deprecated.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.profiling_info"> <code class="descname">profiling_info</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.profiling_info" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a list containing current profiling information.</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> <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/profiling" name="pymongo.database.Database.profiling_info"><em>profiling</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.profiling_level"> <code class="descname">profiling_level</code><span class="sig-paren">(</span><em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.profiling_level" title="Permalink to this definition">¶</a></dt> <dd><p>Get the database’s current profiling level.</p> <p>Returns one of (<a class="reference internal" href="#pymongo.OFF" title="pymongo.OFF"><code class="xref py py-data docutils literal notranslate"><span class="pre">OFF</span></code></a>, <a class="reference internal" href="#pymongo.SLOW_ONLY" title="pymongo.SLOW_ONLY"><code class="xref py py-data docutils literal notranslate"><span class="pre">SLOW_ONLY</span></code></a>, <a class="reference internal" href="#pymongo.ALL" title="pymongo.ALL"><code class="xref py py-data docutils literal notranslate"><span class="pre">ALL</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> <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="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/profiling" name="pymongo.database.Database.profiling_level"><em>profiling</em></a></p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.remove_user"> <code class="descname">remove_user</code><span class="sig-paren">(</span><em>name</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.remove_user" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Remove user <cite>name</cite> from this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</p> <p>User <cite>name</cite> will no longer have permissions to access this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>remove_user is deprecated and will be removed in PyMongo 4.0. Use the dropUser command instead:</p> <div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s2">"dropUser"</span><span class="p">,</span> <span class="s2">"user"</span><span class="p">)</span> </pre></div> </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>name</cite>: the name of the user to remove</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> </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. Deprecated remove_user.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.reset_error_history"> <code class="descname">reset_error_history</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.reset_error_history" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Reset the error history of this database.</p> <p>This method is obsolete: all MongoDB write operations (insert, update, remove, and so on) use the write concern <code class="docutils literal notranslate"><span class="pre">w=1</span></code> and report their errors by default.</p> <p>Calls to <a class="reference internal" href="#pymongo.database.Database.previous_error" title="pymongo.database.Database.previous_error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">previous_error()</span></code></a> will only return errors that have occurred since the most recent call to this method.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.8: </span>Deprecated.</p> </div> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.set_profiling_level"> <code class="descname">set_profiling_level</code><span class="sig-paren">(</span><em>level</em>, <em>slow_ms=None</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.set_profiling_level" title="Permalink to this definition">¶</a></dt> <dd><p>Set the database’s profiling level.</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>level</cite>: Specifies a profiling level, see list of possible values below.</li> <li><cite>slow_ms</cite>: Optionally modify the threshold for the profile to consider a query or operation. Even if the profiler is off queries slower than the <cite>slow_ms</cite> level will get written to the logs.</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> </tbody> </table> <p>Possible <cite>level</cite> values:</p> <table border="1" class="docutils"> <colgroup> <col width="44%" /> <col width="56%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Level</th> <th class="head">Setting</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><a class="reference internal" href="#pymongo.OFF" title="pymongo.OFF"><code class="xref py py-data docutils literal notranslate"><span class="pre">OFF</span></code></a></td> <td>Off. No profiling.</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#pymongo.SLOW_ONLY" title="pymongo.SLOW_ONLY"><code class="xref py py-data docutils literal notranslate"><span class="pre">SLOW_ONLY</span></code></a></td> <td>On. Only includes slow operations.</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#pymongo.ALL" title="pymongo.ALL"><code class="xref py py-data docutils literal notranslate"><span class="pre">ALL</span></code></a></td> <td>On. Includes all operations.</td> </tr> </tbody> </table> <p>Raises <code class="xref py py-class docutils literal notranslate"><span class="pre">ValueError</span></code> if level is not one of (<a class="reference internal" href="#pymongo.OFF" title="pymongo.OFF"><code class="xref py py-data docutils literal notranslate"><span class="pre">OFF</span></code></a>, <a class="reference internal" href="#pymongo.SLOW_ONLY" title="pymongo.SLOW_ONLY"><code class="xref py py-data docutils literal notranslate"><span class="pre">SLOW_ONLY</span></code></a>, <a class="reference internal" href="#pymongo.ALL" title="pymongo.ALL"><code class="xref py py-data docutils literal notranslate"><span class="pre">ALL</span></code></a>).</p> <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="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/profiling" name="pymongo.database.Database.set_profiling_level"><em>profiling</em></a></p> </div> </dd></dl> <dl class="attribute"> <dt id="pymongo.database.Database.system_js"> <code class="descname">system_js</code><a class="headerlink" href="#pymongo.database.Database.system_js" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: <a class="reference internal" href="#pymongo.database.SystemJS" title="pymongo.database.SystemJS"><code class="xref py py-class docutils literal notranslate"><span class="pre">SystemJS</span></code></a> helper for this <a class="reference internal" href="#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a>.</p> <p>See the documentation for <a class="reference internal" href="#pymongo.database.SystemJS" title="pymongo.database.SystemJS"><code class="xref py py-class docutils literal notranslate"><span class="pre">SystemJS</span></code></a> for more details.</p> </dd></dl> <dl class="method"> <dt id="pymongo.database.Database.validate_collection"> <code class="descname">validate_collection</code><span class="sig-paren">(</span><em>name_or_collection</em>, <em>scandata=False</em>, <em>full=False</em>, <em>session=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.Database.validate_collection" title="Permalink to this definition">¶</a></dt> <dd><p>Validate a collection.</p> <p>Returns a dict of validation info. Raises CollectionInvalid if validation fails.</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>name_or_collection</cite>: A Collection object or the name of a collection to validate.</li> <li><cite>scandata</cite>: Do extra checks beyond checking the overall structure of the collection.</li> <li><cite>full</cite>: Have the server do a more thorough scan of the collection. Use with <cite>scandata</cite> for a thorough scan of the structure of the collection and the individual 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> </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.database.Database.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.database.Database.watch" title="Permalink to this definition">¶</a></dt> <dd><p>Watch changes on this database.</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.DatabaseChangeStream" title="pymongo.change_stream.DatabaseChangeStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">DatabaseChangeStream</span></code></a> cursor which iterates over changes on all collections in this database.</p> <p>Introduced in MongoDB 4.0.</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">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.DatabaseChangeStream" title="pymongo.change_stream.DatabaseChangeStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">DatabaseChangeStream</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">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> <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.DatabaseChangeStream" title="pymongo.change_stream.DatabaseChangeStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">DatabaseChangeStream</span></code></a> cursor.</p> </td> </tr> </tbody> </table> <div class="versionadded"> <p><span class="versionmodified">New in version 3.7.</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.database.Database.watch"><em>changeStreams</em></a></p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="pymongo.database.SystemJS"> <em class="property">class </em><code class="descclassname">pymongo.database.</code><code class="descname">SystemJS</code><span class="sig-paren">(</span><em>database</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.SystemJS" title="Permalink to this definition">¶</a></dt> <dd><p><strong>DEPRECATED</strong>: Get a system js helper for the database <cite>database</cite>.</p> <p>SystemJS will be removed in PyMongo 4.0.</p> <dl class="method"> <dt id="pymongo.database.SystemJS.list"> <code class="descname">list</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.database.SystemJS.list" title="Permalink to this definition">¶</a></dt> <dd><p>Get a list of the names of the functions stored in this database.</p> </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="cursor_manager.html" title="previous chapter"><code class="docutils literal notranslate"><span class="pre">cursor_manager</span></code> – Managers to handle when cursors are killed after being closed</a></p> <h4>Next topic</h4> <p class="topless"><a href="driver_info.html" title="next chapter"><code class="docutils literal notranslate"><span class="pre">driver_info</span></code></a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/api/pymongo/database.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="driver_info.html" title="driver_info" >next</a> |</li> <li class="right" > <a href="cursor_manager.html" title="cursor_manager – Managers to handle when cursors are killed after being closed" >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>