In earlier versions of MongoDB, you could use the db.collection.copyTo()
method to copy a collection.
MongoDB has since deprecated that method. Also, starting in version 4.2, MongoDB has removed the eval
command. Given db.collection.copyTo()
wraps eval
, this means that you can’t use either of them if you’re using MongoDB 4.2 or later.
Fortunately, there’s another way to copy a MongoDB collection.
Check for MongoDB Database Tools
In MongoDB, you can clone a collection using the MongoDB Database Tools. Specifically, you can use mongodump
and mongorestore
.
The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB.
If you’re not sure whether you have the MongoDB Database Tools installed, try running the following commands in your Terminal or Command Prompt to check:
mongodump --version
mongorestore --version
That specifically checks for the mongodump
and mongorestore
versions.
If you don’t have them, you can use the installation instructions over at the MongoDB website to install MongoDB Database Tools on to your system.
Clone a Collection to the Same Database
You need to run mongodump
and mongorestore
from your system’s command line (e.g. a new Terminal or Command Prompt window). Don’t run it from the mongo
shell.
Here’s an example of code that clones a collection to the same database:
mongodump --archive --db=PetHotel --collection=pets | mongorestore --archive --nsFrom='PetHotel.pets' --nsTo='PetHotel.pets2'
In this case, we backup the pets
collection in the PetHotel
database, then restore it as pets2
in the same database.
This uses mongodump
to create a backup file of the collection, then mongorestore
to restore that collection under a different name. We did this by dumping the collection to the standard output stream and piping into mongorestore
.
Here’s what each parameter does:
Parameter | Description |
---|---|
--archive | Writes the output to a specified archive file or, if the archive file is unspecified, writes to the standard output (stdout ). In our case, the archive file is unspecified, so it wrote to the standard output. |
--db | Specifies the database that contains the collection we want to clone. In this case, the database is called PetHotel . |
--nsFrom | Specifies the collection in the dump file. This is the collection we want to clone. |
--nsTo | Specifies the collection name that should be used when restored. In our case, we call it pets2 . |
Copy a Collection to Another Database
You can use the same technique to copy the collection to another database. In this case, you wouldn’t even need to change the name of the collection if you don’t want to.
Here’s an example:
mongodump --archive --db=PetHotel --collection=pets | mongorestore --archive --nsFrom='PetHotel.pets' --nsTo='PetHouse.pets'
This is very similar to the previous example, except that our --nsTo
argument specifies PetHouse.pets
as the destination collection.
This means that the cloned collection will be called pets
, and it will be located in the PetHouse
database (instead of the original PetHotel
database).