|
1 | 1 | Copying a Database
|
2 | 2 | ==================
|
3 | 3 |
|
4 |
| -To copy a database within a single mongod process, or between mongod |
5 |
| -servers, simply connect to the target mongod and use the |
| 4 | +MongoDB >= 4.2 |
| 5 | +-------------- |
| 6 | + |
| 7 | +Starting in MongoDB version 4.2, the server removes the deprecated ``copydb`` command. |
| 8 | +As an alternative, users can use ``mongodump`` and ``mongorestore`` (with the ``mongorestore`` |
| 9 | +options ``--nsFrom`` and ``--nsTo``). |
| 10 | + |
| 11 | +For example, to copy the ``test`` database from a local instance running on the |
| 12 | +default port 27017 to the ``examples`` database on the same instance, you can: |
| 13 | + |
| 14 | +#. Use ``mongodump`` to dump the test database to an archive ``mongodump-test-db``:: |
| 15 | + |
| 16 | + mongodump --archive="mongodump-test-db" --db=test |
| 17 | + |
| 18 | +#. Use ``mongorestore`` with ``--nsFrom`` and ``--nsTo`` to restore (with database name change) |
| 19 | + from the archive:: |
| 20 | + |
| 21 | + mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*' |
| 22 | + |
| 23 | +Include additional options as necessary, such as to specify the uri or host, username, |
| 24 | +password and authentication database. |
| 25 | + |
| 26 | +For more info about using ``mongodump`` and ``mongorestore`` see the `Copy a Database`_ example |
| 27 | +in the official ``mongodump`` documentation. |
| 28 | + |
| 29 | +MongoDB <= 4.0 |
| 30 | +-------------- |
| 31 | + |
| 32 | +When using MongoDB <= 4.0, it is possible to use the deprecated ``copydb`` command |
| 33 | +to copy a database. To copy a database within a single ``mongod`` process, or |
| 34 | +between ``mongod`` servers, connect to the target ``mongod`` and use the |
6 | 35 | :meth:`~pymongo.database.Database.command` method::
|
7 | 36 |
|
8 | 37 | >>> from pymongo import MongoClient
|
@@ -39,3 +68,6 @@ but it has been removed.
|
39 | 68 |
|
40 | 69 | .. _copyDatabase function in the mongo shell:
|
41 | 70 | http://docs.mongodb.org/manual/reference/method/db.copyDatabase/
|
| 71 | + |
| 72 | +.. _Copy a Database: |
| 73 | + https://www.mongodb.com/docs/database-tools/mongodump/#std-label-mongodump-example-copy-clone-database |
0 commit comments