updating records from one collection to another mongodb -


i have requirement have copy documents inserted in last 6 hour collection , have in periodic manner. also, if documents exists in target collection have update document source collection.

some stats source collection -

  • source collection has 'jobid' & 'modifieddate' index field.
  • source collection can receive upto 6 million insert/update events per day.

i have referred these links come code - move docs 1 coll another & bulk.find.upsert

var copydocstojobmodel = db.jobsmodel.initializeunorderedbulkop()  var x = 5000 var counter = 0 var lastcopytime = new date(date.now() - 6 * 60 * 60)  var prev_count = db.jobsmodel.count()  db.messages.find({"modifieddate":{$gte: lastcopytime}}).foreach(   function(doc){     delete doc._id     copydocstojobmodel.find({'jobid': doc.jobid}).upsert().updateone(doc);     counter ++     if( counter % x == 0){       copydocstojobmodel.execute()       copydocstojobmodel = db.jobsmodel.initializeunorderedbulkop()     }   }  )  var resp = copydocstojobmodel.execute()  var curr_count = db.jobsmodel.count() [prev_count, curr_count] 

the code working expected have following queries -

  • due the high amount of traffic, don't want block writes/updates while copying documents target collection.
  • we want keep system load low possible while copying records
  • any further optimization can done here make script run faster or consume less resources

thanks in advance.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -