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

查看:84
本文介绍了如何在 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天全站免登陆