Clone a collection in MongoDB

Daryna picture Daryna · Jan 19, 2012 · Viewed 53.3k times · Source

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

Answer

lhagemann picture lhagemann · Jan 19, 2012

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)} );