Mongorestore.exe -host -port -db test -collection collection2 "x:\out\test\collection1.bson"Įxecution time: 18s (9s mongodump + 9s mongorestore) Usage - run the command in command line: mongodump.exe -host -port -db test -collection collection1 -out "x:\out" You can download the tools package via MongoDB Download Center. The mongodump and mongorestore tools are parts of the MongoDB tools package. Usage - run the command in Shell: db.collection1.aggregate()ĭb.collection.aggregate() command reference 4. Can clone collection to the same server only. Usage - run the command in Shell: db.collection1.find().forEach( Doesn't copy collection properties and indexes.Ģ. Can clone collection to the same database only.
Can only be run against MongoDB 4.0 or earlier versions. Usage - run the command in Shell: db.pyTo("collection2")Įxecution time: 7m 5s (tested on MongoDB 4.0 cluster) The results of our comparison you can find here.
Test collection collection1 contains 1 million documents with size about 500Mb. The replica set consists of three MongoDB 4.2 members. We'll test performance on replica set cluster in local network.
We hope this small research will help to find the fastest way for you. We'll measure cloning time, point advantages and disadvantages, compare usability (5-point scale, higher value is better) for each way.
There are several ways how to clone a collection within the same or to a different MongoDB database.