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

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

Is there a simple way to do this?

19条回答
  •  隐瞒了意图╮
    2020-11-28 01:06

    If RAM is not an issue using insertMany is way faster than forEach loop.

    var db1 = connect(':/')
    var db2 = connect(':/')
    
    var _list = db1.getCollection('collection_to_copy_from').find({})
    db2.collection_to_copy_to.insertMany(_list.toArray())
    

提交回复
热议问题