Rishi Raj Arora
Rishi Raj Arora

Reputation: 39

Copying a mongo collection with huge data to another collection?

I have a mongo collection(s) with 2.5 million data and that may grow upto 3 million. I am using spring batch and am trying to copy that collection to another collection. Approaches I have used are as follows :

For less data (< 200 k) it works fine but for 2 million data it hangs the mongo server and the job got failed with Socket Exception

dbMongoTemplate.getDb().getCollection("collection").aggregate(Arrays.asList((DBObject) new BasicDBObject("$out","newCollection")));

This executes a mongo aggregate query db.collection.aggregate({$out : "newCollection"})

This also worked for collections with less data but for larger data set it keeps running until socket time out occurs and fails the job at the end.

Please suggest efficient way to copy data?

Upvotes: 2

Views: 5922

Answers (2)

tejzpr
tejzpr

Reputation: 995

//Fastest way to copy a Collection in MongoDB
db.getCollection('OriginalCollection').aggregate([ { $out: "ClonedCollection" } ]);

This command copied a collection of 2 million records in about 2-3 minutes.

https://gist.github.com/tejzpr/ff37324a8c26d13fef08c318278c0718

Upvotes: 8

profesor79
profesor79

Reputation: 9473

To copy this collection I will sugest using mongodump/mongoexport

mongodump --db databaseName --collection collectionName --out directory-path

then copy directory directory-path and then restore on target machine using

mongorestore --db databaseName --collection collectionName directory-path

Upvotes: 1

Related Questions