mongodb move documents from one collection to another collection

前端 未结 15 1419
感情败类
感情败类 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:08

    From MongoDB 3.0 up, you can use the copyTo command with the following syntax:

    db.source_collection.copyTo("target_collection")
    

    Then you can use the drop command to remove the old collection:

    db.source_collection.drop()
    
    0 讨论(0)
  • 2020-11-30 23:09

    First option (Using mongo dump)

    1.Get a dump from collection

    mongodump -d db -c source_collection

    2.Restore from collection

    mongorestore -d db -c target_collection dir=dump/db_name/source_collection.bson

    Second Option

    Running aggregate

    db.getCollection('source_collection').aggregate([ { $match: {"emailAddress" : "apitester@mailinator.com"} }, { $out: "target_collection" } ])

    Third Option (Slowest)

    Running a through for loop

    db.getCollection('source_collection').find().forEach(function(docs){ db.getCollection('target_collection').insert(docs); }) print("Rolleback Completed!");

    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题