且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MongoDB中将集合从一个数据库复制到另一个数据库

更新时间:2023-02-03 22:58:49

目前,MongoDB中没有命令可以执行此操作.请注意 JIRA票证以及相关功能请求.

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

您可以执行以下操作:

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

请注意,为此,两个数据库需要共享同一mongod才能起作用.

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

除此之外,您可以从一个数据库进行集合的mongodump,然后将集合mongorestore到另一个数据库.

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