Copying a mongo collection with huge data to another collection?

橙三吉。 提交于 2021-02-08 06:13:46

问题


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 :

  • Inside a tasklet, I have Created a ProcessBuilder object and called a shell script which executes a mongo query. Content of shell script is as follows :

    >  mongo $serverURL/$dbName js-file-to-execute.js
    
    // js file contains copy command (db.collection.copyto('newCollection'))
    

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

  • Used a mongo template and executed a query

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?


回答1:


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




回答2:


//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



来源:https://stackoverflow.com/questions/37032415/copying-a-mongo-collection-with-huge-data-to-another-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!