skip to Main Content

I am trying to copy a million document in a collection to another colletion.

> db.source_collection.find().forEach(function(doc) { db.dest_collection.insertOne(doc)});

This works fine. but I wonder it might cause any trouble to operating Database if I loop a million documents.

Can I give a sleep while looping in mongh shell?
Is there any better solution for this?

Please advise me.

2

Answers


  1. You can use bulkWrite() rather than insertOne , it will reduce your time significantly.

    var bulk = db.dest_collection.initializeUnorderedBulkOp();
    var counter = 0;
    db.source_collection.find().forEach(function(doc) {
      bulk.insert(doc);
      counter++;
      if (counter % 1000 == 0) {
        bulk.execute();
        bulk = db.dest_collection.initializeUnorderedBulkOp();
      }
    });
    if (counter % 1000 != 0) {
      bulk.execute();
    }
    

    In this code For every 1000 documents, the code executes the bulk operation using the execute() method, and then re-initializes the bulk operation. Finally, after all documents are processed, the code executes the remaining bulk operation.

    Login or Signup to reply.
  2. If you use the aggregation $out stage, you can copy the documents directly on the server and avoid the network round trip to the client.

    db.source_collection.aggregate([{$out:"destination_collection"}])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search