How to Copy/Clone a Database in MongoDB
-
Use the
db.collection.copyTo()
Command to Copy/Clone a Database in MongoDB -
Use the
db.collection.find().forEach()
Command to Copy/Clone a Database in MongoDB -
Use the
mongodump
andmongorestore
Tools to Copy/Clone a Database in MongoDB -
Use the
mongoexport
andmongoimport
Tools to Copy/Clone a Database in MongoDB - Duplicate Collection Tool of NoSQL Manager for MongoDB
- Copy Collection to Another Database Tool of NoSQL Manager for MongoDB
There are several ways through which the user can clone a collection within the same or to a different MongoDB database. This MongoDB tutorial article will discuss how the user can copy/clone a database in MongoDB and its data.
Use the db.collection.copyTo()
Command to Copy/Clone a Database in MongoDB
This command uses server-side JavaScript to copy all documents from collection
to newCollection
. MongoDB generates a new collection if it does not exist.
You must have all actions on all resources to run db.collection.copyTo()
if permission is enabled. If your business requires a user to run db.collection.copyTo()
, construct a role that grants anyAction
on anyResource
.
No other user should be assigned this role.
Parameter | Type | Description |
---|---|---|
newCollection |
string | The collection’s name where data is written. |
Check field types while using db.collection.copyTo()
to verify that the operation does not delete type information from documents during the BSON to JSON conversion.
Internally, the eval
command is used by the db.collection.copyTo()
method. As a result, the db.collection.copyTo()
operation takes a global lock, which prevents any other read or write activities until the db.collection.copyTo()
operation is finished.
The number of documents copied is returned by copyTo().
If the copy fails, an exception is thrown. Because copyTo()
uses eval
internally, copy actions on the mongod
instance will block all other operations.
For example, the following operation copies all documents from the source
collection into the target
collection.
db.collection1.copyTo("collection2")
- This command can only be run in MongoDB 4.0 or earlier versions.
- It clones the collection to the same database only.
- It is extremely slow.
- It does not copy collection properties and indexes.
Use the db.collection.find().forEach()
Command to Copy/Clone a Database in MongoDB
This command iterates through the cursor, applying a JavaScript function to each document. The prototype form of the forEach()
method is as follows:
db.collection.find().forEach(<function>)
The forEach()
method has the following parameter:
Parameter | Type | Description |
---|---|---|
function |
JavaScript | A JavaScript function to apply to each document from the cursor. The <function> signature includes a single argument passed the current document to process. |
Example:
db.collection1.find().forEach(
function(docs){
db.collection2.insert(docs);
})
- This command can clone collection to the same server only.
- It is very slow.
- It does not copy collection properties and indexes.
Use the mongodump
and mongorestore
Tools to Copy/Clone a Database in MongoDB
To duplicate a database in earlier versions of MongoDB, you might use the copyDB
command or its helper method, db.copyDatabase()
. But, MongoDB has subsequently deprecated these.
Also, starting with version 4.2, MongoDB deleted the copydb
command and the db.copyDatabase()
method, so if you are using MongoDB 4.2 or later, you would not be able to utilize them. There is, however, a different approach to duplicating a MongoDB database.
The MongoDB Database Tools can be used to clone a database in MongoDB. You can use the commands mongodump
and mongorestore
.
MongoDB Database Tools is a collection of command-line tools for dealing with MongoDB. If you’re not sure if you have the MongoDB Database Tools installed, use your Terminal or Command Prompt to check:
mongodump --version
mongorestore --version
This code mainly looks for the versions of mongodump
and mongorestore.
If you do not already have them, you can install MongoDB Database Tools on your PC by following the installation instructions found on the MongoDB website.
You must use your system’s command line to run mongodump
and mongorestore
(e.g., a new Terminal or Command Prompt window). It should not be executed from the mongo shell.
Here’s an example of database cloning code:
mongodump --archive --db=CatHotel | mongorestore --archive --nsFrom='CatHotel.*' --nsTo='CatHouse.*'
In this case, we backup the CatHotel
database, then restore all of its collections to a database called CatHouse
. In other words, we cloned the CatHotel
database as CatHouse
.
This uses mongodump
to produce a database backup file, followed by mongorestore
to restore the database under a new name. First, the database was dumped into the standard output stream and piped into mongorestore.
Here is what each parameter does:
Parameter | Description |
---|---|
archive |
It will write the output to a specified archive file or the standard output (stdout ) if the archive file is unspecified. In your case, the archive file is unspecified so that it will write to the standard output. |
db |
It will specify a database to backup. In this case, you will backup the CatHotel database. |
nsFrom |
It will specify the collection in the dump file. The asterisk wildcard (* ) means all collections. |
nsTo |
It specifies the collection name that needs to be used in the restored database. |
You can also dump all databases with mongodump
and run it without any parameters. However, the local
and config
databases are not included in the dump when you do that.
- It is a high-speed method.
- It can clone collections to another database and server.
Use the mongoexport
and mongoimport
Tools to Copy/Clone a Database in MongoDB
The MongoDB tools package includes the mongoexport
and mongoimport
tools. The tools package is available for download from the MongoDB Download Center.
For example, run the command below in the command line:
mongoexport.exe /host:<host> /port:<port> /db:test /collection:collection1 /out:collection1.json
mongoimport.exe /host:<host> /port:<port> /db:test /collection:collection2 /file:collection1.json
- It is a fast method.
- It can clone collections to a different database and server.
- It does not copy collection properties and indexes.
Duplicate Collection Tool of NoSQL Manager for MongoDB
Duplicate Collection is a professional feature. It allows duplicating collection very quickly within the same database.
Right-click on the collection1
collection in DB Explorer and select Duplicate collection1
Collection item in the popup menu.
Specify destination collection name, duplication parameters, and click Duplicate.
- It is a high-speed method.
- It copies collection properties and indexes.
- It can clone collections to the same database only.
Copy Collection to Another Database Tool of NoSQL Manager for MongoDB
Copy Collection to another Database is a professional feature of NoSQL Manager for MongoDB Pro. It allows for copying one or many collections between databases and servers.
Right-click on collection1
in DB Explorer and select Copy collection1
Collection to another Database item in the popup menu.
Specify the destination database and additional parameters and click Execute.
- It is a fast method.
- It copies collection properties and indexes.
- It can copy the collection to another database and server.
- It can copy several collections at once.
- It can not rename the collection.