mongodb mongodump 忽略一些指定的集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15962496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mongodump ignore some specified collections
提问by sashimi
I was trying to backup my mongo database on the product sever.and then restore then back to the staging server. and here comes some problem, there are a lot of collections in db, I want to igonre some collections that I don't want to restore on staging server.
我试图在产品服务器上备份我的 mongo 数据库。然后恢复然后返回到登台服务器。这里出现了一些问题,数据库中有很多集合,我想忽略一些我不想在登台服务器上恢复的集合。
I can approach this by dumpping the staging db, dumpping the producting db, and then restore the prodct to staging useing --dropoption. and restore the specified collections in staging db. uh..it's really bad.
我可以通过转储暂存数据库,转储生产数据库,然后使用--drop选项将产品恢复到暂存来解决这个问题。并在暂存数据库中恢复指定的集合。呃。。真的很糟糕。
1. dump producting db
1.转储生产数据库
mongodump --host product-server-host --username abcd --password bcda -d db -o pruduct-dump-dir
2. dump staging db
2.转储暂存数据库
mongodump --host staging-server-host --username abcd --password bcda -d db -o staging -dump-dir
3. restore all collection, then restore the collection backrestore pruduct-dump-dir to staging server
3.恢复所有集合,然后将集合恢复到restore pruduct-dump-dir到staging server
mongorestore --host staging-server-host --username abcd --password bcda --drop pruduct-dump-dir
mongorestore --host staging-server-host --username abcd --password bcda --drop --collection coll pruducting-dump-dir
Is there any option like ignore-collection when I'm dumpping? any suggestion will be appreciated :3
当我倾倒时,是否有类似 ignore-collection 的选项?任何建议将不胜感激:3
回答by Daniel Pérez Rada
Now available from version 3.0.0
现在从 3.0.0 版开始可用
--excludeCollection <collection_name>
--excludeCollectionsWithPrefix <collection_prefix>
Repeat to exclude more than 1
重复以排除超过 1 个
Checkout the documentation
查看文档
回答by Nick
回答by Nataniel Paiva
mongodump --db test --excludeCollection=users --excludeCollection=salaries
回答by David
As of Mongo 3.4, you can now specify an --nsExclude <namespace pattern>
option when restoring from a Mongo database dump, which will exclude the specified namespaces from the restore operation. This is particularly useful when the mongodump
operation has already occurred.
从 Mongo 3.4 开始,您现在--nsExclude <namespace pattern>
可以在从 Mongo 数据库转储还原时指定一个选项,这将从还原操作中排除指定的命名空间。这在mongodump
操作已经发生时特别有用。
Official documentation here: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-nsexclude
官方文档在这里:https: //docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-nsexclude
You can exclude multiple collections with wildcards:
您可以使用通配符排除多个集合:
mongorestore --db test --nsExclude 'test.*_tmp'
Or alternatively, specifying multiple --nsExclude
options work as well:
或者,也可以指定多个--nsExclude
选项:
mongorestore --db test --nsExclude 'test.collection1' --nsExclude 'test.collection2'
回答by Ayush Lal Shrestha
I had to do the same while backing up a mongo db. If you use python (or any other language), you can use similar approach as well. After doing the mongodump, you simple have to remove the unwanted collection's bson & the metadata.json files.
在备份 mongo db 时,我必须做同样的事情。如果您使用 python(或任何其他语言),您也可以使用类似的方法。执行 mongodump 后,您只需删除不需要的集合的 bson 和 metadata.json 文件。
import os
EXCLUDE_COLLECTIONS = ['collection_1', 'collection_2']
db_dump_path = "/Path/to/mongodump"
db_name = "name_of_db"
for collection_name in EXCLUDE_COLLECTIONS:
bson_file_path = os.path.join(db_dump_path, db_name, '{}.bson'.format(collection_name)
meta_file_path = os.path.join(db_dump_path, db_name, '{}.metadata.json'.format(collection_name)
if os.path.exists(bson_file_path) and os.path.exists(meta_file_path):
os.remove(bson_file_path)
os.remove(meta_file_path)