如何在MongoDB中将集合从一个数据库复制到另一个数据库 [英] How to copy a collection from one database to another in MongoDB

查看:2417
本文介绍了如何在MongoDB中将集合从一个数据库复制到另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以做到这一点?

Is there a simple way to do this?

推荐答案

目前,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.

这篇关于如何在MongoDB中将集合从一个数据库复制到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆