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

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

Is there a simple way to do this?

19条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 01:06

    At the moment there is no command in MongoDB that would do this. Please note the JIRA ticket with related feature request.

    You could do something like:

    db..find().forEach(function(d){ db.getSiblingDB('')[''].insert(d); });
    

    Please note that with this, the two databases would need to share the same mongod for this to work.

    Besides this, you can do a mongodump of a collection from one database and then mongorestore the collection to the other database.

提交回复
热议问题