Best way to clone MongoDB collection

7 different ways to clone MongoDB collection

There are several ways how to clone a collection within the same or to a different MongoDB database.
We'll measure cloning time, point advantages and disadvantages, compare usability (5-point scale, higher value is better) for each way.
We hope this small research will help to find the fastest way for you.

We'll test performance on replica set cluster in local network. The replica set consists of three MongoDB 4.2 members. Test collection collection1 contains 1 million documents with size about 500Mb.

We'll consider seven different ways:

  1. db.collection.copyTo() command
  2. db.collection.find().forEach() command
  3. db.collecion.aggregate() command
  4. mongodump and mongorestore tools
  5. mongoexport and mongoimport tools
  6. Duplicate collection tool of NoSQL Manager for MongoDB
  7. Copy collection to another database tool of NoSQL Manager for MongoDB

The results of our comparison you can find in this summary.

1. db.collection.copyTo() command

Usage - run the command in Shell:

db.collection1.copyTo("collection2")

Execution time: 7m 5s (tested on MongoDB 4.0 cluster)
Pros:
+ Usability: 4.
Cons:
- Can only be run against MongoDB 4.0 or earlier versions.
- Can clone collection to the same database only.
- Very slow.
- Doesn't copy collection properties and indexes.

2. db.collection.find().forEach() command

Usage - run the command in Shell:

db.collection1.find().forEach(
function(docs){
db.collection2.insert(docs);
})

Execution time: 7m 45s
Pros:
+ Usability: 4.
Cons:
- Can clone collection to the same server only.
- Very slow.
- Doesn't copy collection properties and indexes.

3. db.collecion.aggregate() command

Usage - run the command in Shell:

db.collection1.aggregate([{ $match: {} }, { $out: "collection2" }])

Execution time: 15s
Pros:
+ Very fast.
+ Usability: 4.
Cons:
- Can clone collection to the same database only.
- Doesn't copy collection properties and indexes.

db.collection.aggregate() command reference

4. mongodump and mongorestore tools

The mongodump and mongorestore tools are parts of the MongoDB tools package.
You can download the tools package via MongoDB Download Center.

Usage - run the command in command line:

mongodump.exe  --host <host> --port <port> --db test --collection collection1 --out "x:\out"
mongorestore.exe  --host <host> --port <port> --db test --collection collection2 "x:\out\test\collection1.bson"

Execution time: 18s (9s mongodump + 9s mongorestore)
Pros:
+ Very fast.
+ Can clone collection to another database and server.
Cons:
- Usability: 3.

5. mongoexport and mongoimport tools

The mongoexport and mongoimport tools are parts of the MongoDB tools package.
You can download the tools package via MongoDB Download Center.

Usage - run the command in 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

Execution time: 44s (27s mongoexport + 17s mongoimport)
Pros:
+ Fast.
+ Can clone collection the a diffretent database and server.
Cons:
- Usability: 3.
- Doesn't copy collection properties and indexes.

6. Duplicate collection tool of NoSQL Manager for MongoDB

Duplicate Collection is an internal tool of NoSQL Manager for MongoDB Freeware and NoSQL Manager for MongoDB Pro. It allows to duplicate collection very quickly within the same database.

Right-click on collection1 collection in DB Explorer and select Duplicate 'collection1' Collection... item in the popup menu.

Click 'Duplicate collection1 Collection' in DB Explorer context menu

Specify destination collection name, duplication parameters and click Duplicate.

Type 'collection2' in destination collection name field

Execution time: 15s
Pros:
+ Very fast.
+ Copies collection properties and indexes.
+ Free for non-commercial use.
+ Usability: 5.
Cons:
- Can clone collection to the same database only.

To use Duplicate Collection you can download NoSQL Manager for MongoDB Freeware or buy NoSQL Manager for MongoDB Pro.
Duplicate Collection as a part of NoSQL Manager for MongoDB Freeware is free for non-commercial use.

7. 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. It allows to copy one or many collections between databases and servers.

Right-click on collection1 collection in DB Explorer and select Copy 'collection1' Collection to another Database... item in the popup menu.

Click 'Copy collection1 Collection to another Database' in DB Explorer context menu

Specify destination database, additional parameters and click Execute.

Destination database and optional performance parameters

Execution time: 35s
Pros:
+ Fast.
+ Copies collection properties and indexes.
+ Can copy collection to another database and server.
+ Can copy several collections at once.
+ Usability: 5.
Cons:
- Can't rename collection.
- Available in Pro version only.

Summary

Duplicates indexes Can duplicate to another database Can duplicate to another server Can duplicate many collection at once Duplication time
db.collection.copyTo 7m 5s
db.collection.find().forEach 7m 45s
db.collection.aggregate 15s
mongodump and mongorestore 18s
mongoexport and mongoimport 44s
Duplicate collection tool 15s
Copy collections tool 35s