在现有 mongodb 中恢复单个集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28640281/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:18:44  来源:igfitidea点击:

Restoring single collection in an existing mongodb

mongodbubuntu-14.04

提问by JonRed

I'm failing miserably to be able to restore a single collection into an existing database. I'm running Ubuntu 14.04 with mongo version 2.6.7 There is a dump/mydbname/contents.bson based off my home directory.

我失败了,无法将单个集合恢复到现有数据库中。我正在使用 mongo 版本 2.6.7 运行 Ubuntu 14.04 有一个基于我的主目录的 dump/mydbname/contents.bson。

If I run

如果我跑

mongorestore --collection contents --db mydbname

Then I get:

然后我得到:

connected to: 127.0.0.1
don't know what to do with file [dump]

If I add in the path

如果我在路径中添加

mongorestore --collection contents --db mydbname --dbpath dump/mydbname

Then I get

然后我得到

If you are running a mongod on the same path you should connect to that instead of direct data file access

I've tried various other combinations, options, etc. and just can't puzzle it out, so I'm coming to the community for help!

我已经尝试了各种其他组合、选项等,但无法解开,所以我来社区寻求帮助!

回答by Abhay PS

If you want to restore a single collection then you have to specifiy the dump file of the collection. The dump file of the collection is found in the 'dump/dbname/' folder. So assuming your dump folder is in your current working directory, the command would go something like -

如果要恢复单个集合,则必须指定集合的​​转储文件。集合的转储文件位于“dump/dbname/”文件夹中。因此,假设您的转储文件夹位于您当前的工作目录中,该命令将类似于 -

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson

回答by wordsforthewise

I think this is now done with the --nsIncludeoption:

我认为现在可以通过以下--nsInclude选项完成:

mongorestore --nsInclude test.purchaseorders dump/

mongorestore --nsInclude test.purchaseorders dump/

dump/is the folder with your mongodump data, testis the db, and purchaseordersis the collection.

dump/是包含 mongodump 数据的文件夹,test是 db,purchaseorders是集合。

https://docs.mongodb.com/manual/reference/program/mongorestore/

https://docs.mongodb.com/manual/reference/program/mongorestore/

回答by Krutarth Chokshi

Steps to restore specific collection in the mongodb.

在 mongodb 中恢复特定集合的步骤。

1) Go to the directory where your dump folder exists.

1) 转到您的转储文件夹所在的目录。

2) Execute following command by modifying according to your db name and your collection name.

2)根据您的数据库名称和集合名称进行修改,执行以下命令。

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson

If you get Failed: yourdbname.collection.name: error creating indexes for collection.name: createIndex error: The field 'safe' is not valid for an index specification error, then you can use following command:

如果你得到Failed: yourdbname.collection.name: error creating indexes for collection.name: createIndex error: The field 'safe' is not valid for an index specification error,那么你可以使用以下命令:

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson --noIndexRestore

回答by Alexander Mills

If you are restoring multiple collections, you can use a loop:

如果要恢复多个集合,则可以使用循环:

for file in "$HOME/mongodump/dev/<your-db>/"* ; do

  if [[ "$file" != "*metadata*" && "$file" != "system.*" && "$file" != "locks.*" ]]; then

    file="$(basename "$file”)"

    mongorestore \
        --db cdt_dev \
        --collection "${file%.*}" \   # filename w/o extension
        --host "<your-host>" \
        --authenticationDatabase "<your-auth-db>" \
        -u "user" \
        -p "pwd" \
        "$HOME/mongodump/dev/<your-db>/$file"

  fi;

done