<!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>Copying a Database — 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="Bulk Write Operations" href="bulk.html" /> <link rel="prev" title="Collations" href="collations.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="bulk.html" title="Bulk Write Operations" accesskey="N">next</a> |</li> <li class="right" > <a href="collations.html" title="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" accesskey="U">Examples</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="copying-a-database"> <h1>Copying a Database<a class="headerlink" href="#copying-a-database" title="Permalink to this headline">¶</a></h1> <p>To copy a database within a single mongod process, or between mongod servers, simply connect to the target mongod and use the <a class="reference internal" href="../api/pymongo/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> method:</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">MongoClient</span> <span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">'target.example.com'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">client</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s1">'copydb'</span><span class="p">,</span> <span class="go"> fromdb='source_db_name',</span> <span class="go"> todb='target_db_name')</span> </pre></div> </div> <p>To copy from a different mongod server that is not password-protected:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">client</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s1">'copydb'</span><span class="p">,</span> <span class="go"> fromdb='source_db_name',</span> <span class="go"> todb='target_db_name',</span> <span class="go"> fromhost='source.example.com')</span> </pre></div> </div> <p>If the target server is password-protected, authenticate to the “admin” database:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">'target.example.com'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">username</span><span class="o">=</span><span class="s1">'administrator'</span><span class="p">,</span> <span class="gp">... </span> <span class="n">password</span><span class="o">=</span><span class="s1">'pwd'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">client</span><span class="o">.</span><span class="n">admin</span><span class="o">.</span><span class="n">command</span><span class="p">(</span><span class="s1">'copydb'</span><span class="p">,</span> <span class="go"> fromdb='source_db_name',</span> <span class="go"> todb='target_db_name',</span> <span class="go"> fromhost='source.example.com')</span> </pre></div> </div> <p>See the <a class="reference internal" href="authentication.html"><span class="doc">authentication examples</span></a>.</p> <p>If the <strong>source</strong> server is password-protected, use the <a class="reference external" href="http://docs.mongodb.org/manual/reference/method/db.copyDatabase/">copyDatabase function in the mongo shell</a>.</p> <p>Versions of PyMongo before 3.0 included a <code class="docutils literal notranslate"><span class="pre">copy_database</span></code> helper method, but it has been removed.</p> </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="collations.html" title="previous chapter">Collations</a></p> <h4>Next topic</h4> <p class="topless"><a href="bulk.html" title="next chapter">Bulk Write Operations</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/examples/copydb.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="bulk.html" title="Bulk Write Operations" >next</a> |</li> <li class="right" > <a href="collations.html" title="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" >Examples</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>