Clone a collection in 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)} );

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.


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