mongodb 使用单个命令恢复多个集合

Meh*_*amo 1 mongodb restore

如何在 mongodb 中一次恢复多个集合。

我试过了

mongorestore -c Role -c UserAccount -c Permission -d movie-app dump/ 
Run Code Online (Sandbox Code Playgroud)

我有一个错误 file dump is a directory, not a bson file

我可以一次恢复单个集合,我必须指定 bson 文件,例如

  mongorestore -c UserAccount -d movie-app dump/movie-app/UserAccount.bson 
Run Code Online (Sandbox Code Playgroud)

我需要知道如何使用一个命令恢复多个集合。

The*_*Guy 7

尝试使用这些参数来包含或排除集合

在恢复期间,您可以忽略不需要的集合。

例如:

转储所有集合

mongodump --db bhuvi --out /tmp

drwxr-xr-x 2 root root 4096 Dec 20 18:09 ./
drwxrwxrwt 9 root root 4096 Dec 20 18:25 ../
-rw-r--r-- 1 root root  125 Dec 20 18:09 coll1.bson
-rw-r--r-- 1 root root  125 Dec 20 18:09 coll1.metadata.json
-rw-r--r-- 1 root root   83 Dec 20 18:09 coll2.bson
-rw-r--r-- 1 root root  125 Dec 20 18:09 coll2.metadata.json
-rw-r--r-- 1 root root  125 Dec 20 18:09 coll3.bson
-rw-r--r-- 1 root root  125 Dec 20 18:09 coll3.metadata.json
Run Code Online (Sandbox Code Playgroud)

我的转储文件夹有 3 个集合。

  1. coll1
  2. coll2
  3. coll3

恢复 coll2 和 coll3 集合

我想在测试数据库中恢复 coll2 和 coll3。

mongorestore --db test --nsExclude 'test.*1' /tmp/bhuvi/

2017-12-20T18:25:50.299+0000    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2017-12-20T18:25:50.299+0000    building a list of collections to restore from /tmp/bhuvi dir
2017-12-20T18:25:50.299+0000    reading metadata for test.coll3 from /tmp/bhuvi/coll3.metadata.json
2017-12-20T18:25:50.312+0000    restoring test.coll3 from /tmp/bhuvi/coll3.bson
2017-12-20T18:25:50.313+0000    reading metadata for test.coll2 from /tmp/bhuvi/coll2.metadata.json
2017-12-20T18:25:50.326+0000    restoring test.coll2 from /tmp/bhuvi/coll2.bson
2017-12-20T18:25:50.327+0000    no indexes to restore
2017-12-20T18:25:50.327+0000    finished restoring test.coll3 (3 documents)
2017-12-20T18:25:50.327+0000    no indexes to restore
2017-12-20T18:25:50.328+0000    finished restoring test.coll2 (2 documents)
2017-12-20T18:25:50.328+0000    done
Run Code Online (Sandbox Code Playgroud)

检查集合

> use test
switched to db test
> show collections
coll2
coll3
Run Code Online (Sandbox Code Playgroud)

用于恢复具有相似名称的集合

mongorestore --db test --nsInclude 'test.coll*' /tmp/bhuvi/

恢复不同模式的集合

对此没有直接的方法,但我们可以创建一个循环来恢复它。

创建一个包含我们需要恢复的集合列表的文件

vi collections
#add the collections names and save.
coll1
coll2
Run Code Online (Sandbox Code Playgroud)

用于循环恢复的 shell 脚本

#!/bin/bash
input="collcetions"
backup_path="/tmp/mydb"
while IFS= read -r col_name
do
  mongorestore --db sqladmin --collection $col_name $backup_path/$col_name.bson
done < "$input"
Run Code Online (Sandbox Code Playgroud)

root@lin_sql1:/home/ubuntu# ./restore

2017-12-21T04:56:36.596+0000    checking for collection data in /tmp/mydb/coll1.bson
2017-12-21T04:56:36.596+0000    reading metadata for sqladmin.coll1 from /tmp/mydb/coll1.metadata.json
2017-12-21T04:56:36.616+0000    restoring sqladmin.coll1 from /tmp/mydb/coll1.bson
2017-12-21T04:56:36.677+0000    no indexes to restore
2017-12-21T04:56:36.677+0000    finished restoring sqladmin.coll1 (3 documents)
2017-12-21T04:56:36.677+0000    done
2017-12-21T04:56:36.686+0000    checking for collection data in /tmp/mydb/coll2.bson
2017-12-21T04:56:36.686+0000    reading metadata for sqladmin.coll2 from /tmp/mydb/coll2.metadata.json
2017-12-21T04:56:36.699+0000    restoring sqladmin.coll2 from /tmp/mydb/coll2.bson
2017-12-21T04:56:36.760+0000    no indexes to restore
2017-12-21T04:56:36.760+0000    finished restoring sqladmin.coll2 (2 documents)
2017-12-21T04:56:36.760+0000    done
Run Code Online (Sandbox Code Playgroud)

检查集合

> use sqladmin
switched to db sqladmin
> show collections
coll1
coll2
Run Code Online (Sandbox Code Playgroud)

  • 为什么不只运行一次命令并多次指定 --nsInclude,每个命名空间一次? (2认同)