How to copy a collection from one database to another in MongoDB

前端 未结 19 1585
無奈伤痛
無奈伤痛 2020-11-28 00:22

Is there a simple way to do this?

19条回答
  •  暖寄归人
    2020-11-28 00:56

    for huge size collections, you can use Bulk.insert()

    var bulk = db.getSiblingDB(dbName)[targetCollectionName].initializeUnorderedBulkOp();
    db.getCollection(sourceCollectionName).find().forEach(function (d) {
        bulk.insert(d);
    });
    bulk.execute();
    

    This will save a lot of time. In my case, I'm copying collection with 1219 documents: iter vs Bulk (67 secs vs 3 secs)

提交回复
热议问题