Best way to clone MongoDB collection

Overview

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 here.

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.

db.collection.copyTo() command reference (this command is deprecated since version 3.0.)

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 a professional feature of 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.

DB Explorer: Duplicate Collection tool

Specify destination collection name, duplication parameters and click Duplicate.

Duplicate Collection tool

Execution time: 15s
Pros:
+ Very fast.
+ Copies collection properties and indexes.
+ Usability: 5.
Cons:
- Can clone collection to the same database only.
- Available in Pro version only.

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 Pro. 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.

DB Explorer: Duplicate Collection tool

Specify destination database, additional parameters and click Execute.

Copy Collection tool

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