mongodb move documents from one collection to another collection

前端 未结 15 1557
感情败类
感情败类 2020-11-30 22:10

How can documents be moved from one collection to another collection in MongoDB?? For example: I have lot of documents in

15条回答
  •  余生分开走
    2020-11-30 23:13

    Update 2

    Please do NOT upvote this answer any more. As written @jasongarber's answer is better in any aspect.

    Update

    This answer by @jasongarber is a safer approach and should be used instead of mine.


    Provided I got you right and you want to move all documents older than 1 month, and you use mongoDB 2.6, there is no reason not to use bulk operations, which are the most efficient way of doing multiple operations I am aware of:

    > var bulkInsert = db.target.initializeUnorderedBulkOp()
    > var bulkRemove = db.source.initializeUnorderedBulkOp()
    > var date = new Date()
    > date.setMonth(date.getMonth() -1)
    > db.source.find({"yourDateField":{$lt: date}}).forEach(
        function(doc){
          bulkInsert.insert(doc);
          bulkRemove.find({_id:doc._id}).removeOne();
        }
      )
    > bulkInsert.execute()
    > bulkRemove.execute()
    

    This should be pretty fast and it has the advantage that in case something goes wrong during the bulk insert, the original data still exists.


    Edit

    In order to prevent too much memory to be utilized, you can execute the bulk operation on every x docs processed:

    > var bulkInsert = db.target.initializeUnorderedBulkOp()
    > var bulkRemove = db.source.initializeUnorderedBulkOp()
    > var x = 10000
    > var counter = 0
    > var date = new Date()
    > date.setMonth(date.getMonth() -1)
    > db.source.find({"yourDateField":{$lt: date}}).forEach(
        function(doc){
          bulkInsert.insert(doc);
          bulkRemove.find({_id:doc._id}).removeOne();
          counter ++
          if( counter % x == 0){
            bulkInsert.execute()
            bulkRemove.execute()
            bulkInsert = db.target.initializeUnorderedBulkOp()
            bulkRemove = db.source.initializeUnorderedBulkOp()
          }
        }
      )
    > bulkInsert.execute()
    > bulkRemove.execute()
    

提交回复
热议问题