通过stdin mongorestore到具有不同名称的数据库 [英] Mongorestore through stdin to db with different name

查看:97
本文介绍了通过stdin mongorestore到具有不同名称的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找到一种方法来做,但是不能,而且我觉得没有一种简单的方法可以完成我想要的事情.

I have been trying to find a way to do this but cannot, and I have a feeling there isn't an easy way to do what I want.

我一直在使用进行测试mongodump |mongorestore 作为避免创建实际存储文件集的一种方法,这在基于云的服务中非常有用.到目前为止,我一直在通过指定相同的db进行测试,尽管从技术上讲它不是必需的,就像这样……

I have been testing using mongodump | mongorestore as a way to avoid creating an actual stored set of files, which is very useful on a cloud-based service. So far, I have been testing this by specifying the same db, although it technically isn't necessary, like so...

mongodump -h hostname -d dumpdb --excludeCollection bigcollection --archive | mongorestore --archive -d dumpdb --drop
mongodump -h hostname -d dumpdb -c bigcollection --archive --queryFile onlythese.json | mongorestore --archive -d dumpdb -c bigcollection --drop

我发现这些选项最适合我;当我尝试在单个数据库中指定 -o-选项时,删除了-archive 时,我遇到了一些问题,但是由于这样做有效,所以我没有搞砸了.由于我正在还原到同一数据库,并且因为仅还原了当时所在的数据库,所以我意识到可以在其中删除 -d -c 两个mongorestore命令.但是,这很容易做到,并且是下一步的准备工作...我要做的就是分两步将指定的数据库还原到其他名称的数据库,就像这样...

I have found that these options work best for me; when I attempt to specify the -o - option with the single db, with the --archive removed, I was having some issues, but since this worked, I didn't mess with it. Since I was restoring to the same db and because only the collections that were in there at the time were restored, I realize I can knock off the -d and -c in both mongorestore commands. But, it was easy to do, and is the set up for the next step... All I wanted to do was restore the specified db in two steps, to a db of a different name, like so...

mongodump -h hostname -d dumpdb --excludeCollection bigcollection --archive | mongorestore --archive -d restoredb --drop
mongodump -h hostname -d dumpdb -c bigcollection --archive --queryFile onlythese.json | mongorestore --archive -d restoredb -c bigcollection --drop

转储工作正常,但还原无效.基于有限的文档,我的假设是转储db和restore db必须相同才能正常工作.如果 mongorestore 中指定的数据库不在转储中,它将无法恢复.我觉得这很烦人.我的另一个想法是按原样还原数据库,然后将其复制到其他数据库.我曾考虑过使用其他工具,例如 mongo-dump-stream ,但考虑到一切都是到目前为止,我一直在努力工作,我希望它能与默认工具一起使用.另一方面,我确实将存档转储到文件中(例如:> dumpdb.tar ),并尝试从那里还原(例如-archive = dumpdb.tar ),这就是我确认数据库需要保存在转储中的方式.

The dump works fine, but restore does not. Based on the limited documentation, my assumption is that the dump db and restore db need to be the same for this to work; if the db specified in mongorestore is not in the dump, it just won't restore. I find this to be rather annoying; my other thought was to restore the db as is, and just copy it to the other db. I have thought about using other tools such as mongo-dump-stream, but considering everything was working swimmingly so far, I was hoping that it would work with the default tools. On another point, I did dump the archive to a file (like so: > dumpdb.tar) and attempt to restore from there (like so --archive=dumpdb.tar) which is how I confirmed that the db needed to be in the dump.

任何建议/评论/hacks都将受到欢迎.

Any suggestions/comments/hacks would be welcome.

推荐答案

mongorestore 的3.4版开始,您可以使用-nsFrom -nsTo 选项,它们提供了一种基于模式的方式来在源和目标之间操纵集合和/或数据库的名称.

As of version 3.4 of mongorestore, you can accomplish this using the --nsFrom and --nsTo options, which provide a pattern-based way to manipulate the names of your collections and/or dbs between the source and destination.

例如,要从名为dumpdb的数据库转储到名为Restoredb的新数据库中:

For example, to dump from a database named dumpdb into a new database named restoredb:

mongodump -h hostname -d dumpdb --archive | mongorestore --archive --nsFrom "dumpdb.*" --nsTo "restoredb.*" --drop

来自mongodb文档的更多信息:https://docs.mongodb.com/manual/reference/program/mongorestore/#change-collections-namespaces-during-restore

More from the mongodb docs: https://docs.mongodb.com/manual/reference/program/mongorestore/#change-collections-namespaces-during-restore

这篇关于通过stdin mongorestore到具有不同名称的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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