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

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

Is there a simple way to do this?

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 00:49

    In my case, I had to use a subset of attributes from the old collection in my new collection. So I ended up choosing those attributes while calling insert on the new collection.

    db..find().forEach(function(doc) { 
        db..insert({
            "new_field1":doc.field1,
            "new_field2":doc.field2,
            ....
        })
    });`
    

提交回复
热议问题