Clone a collection in MongoDB

MongodbMongo Collection

Mongodb Problem Overview


I want to clone a MongoDB collection and save it on the same server with a different name. So for example right now I have the following collections: demo1.categories, demo1.users and demo2.users.

I want to have a "demo2.categories" which is identical to "demo1.categories". (It just has a different name.)

Mongodb Solutions


Solution 1 - Mongodb

Yet again the MongoDB documentation comes to the rescue

assuming that the collection actually is named "demo1.categories":

db.demo1.categories.find().forEach( function(x){db.demo2.categories.insert(x)} );

Solution 2 - Mongodb

The most simple & efficient way is by using copyTo(), so you can use:

db.source.copyTo("target"); 

& if "target" doesn't exist, it will be created

-- Update --

According to CopyTo Documentation, Because copyTo() uses eval internally, the copy operations will block all other operations on the mongod instance. So it shouldn't be used on production environment.

-- Update --

Because CopyTo() uses eval() internally & eval() is deprecated since version 3.0, so CopyTo() is also deprecated since version 3.0.

Solution 3 - Mongodb

This is the fastest way to clone your collection:

mongoexport -d db_name -c src_collection | mongoimport -d db_name -c dst_collection --drop

it will clone src_collection in db_name to dst_collection. Or you can do it in two steps on bson level:

mongodump -d db_name -c src_collection
mongorestore --drop -d db_name -c dst_collection ./dump/db_name/src_collection.bson

Solution 4 - Mongodb

The fastest option is

db.myoriginal.aggregate([ { $out: "mycopy" } ])

Solution 5 - Mongodb

there already has a command for this.

Copy a single collection from one server to another. http://www.mongodb.org/display/DOCS/cloneCollection+Command

Solution 6 - Mongodb

If you're concerned about speed then I found that by using aggregate with $project and $out to be a 100 times faster, not sure if there are restrictions though, but you would have to create a set of fields that you'd want to copy For example:

// Set of fields in the categories collection
var setOfFields = {field1:1, field2:1.......}
db.demo1.categories.aggregate([{ "$project": setOfFields},{ $out: "demo2.categories"}]);

This copies (projects) the selected set of fields for all documents from demo1.categories to demo2.categories

Solution 7 - Mongodb

Don't use db.cloneCollection() method, it is depreciated from the current version 4.2 instead try to use mongoexport.

depreciated collection method

Solution 8 - Mongodb

In the mongo console, you can do the following as well, where db_host is the machine where db_host has the db with the collection that you want to clone.

use db.cloneCollection(, )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarynaView Question on Stackoverflow
Solution 1 - MongodblhagemannView Answer on Stackoverflow
Solution 2 - MongodbAbdelHadyView Answer on Stackoverflow
Solution 3 - MongodbTutankhamenView Answer on Stackoverflow
Solution 4 - MongodbtejzprView Answer on Stackoverflow
Solution 5 - MongodbkerwinView Answer on Stackoverflow
Solution 6 - MongodbChenna VView Answer on Stackoverflow
Solution 7 - MongodbAjayView Answer on Stackoverflow
Solution 8 - Mongodbuser1588040View Answer on Stackoverflow