mongodb restore multiple collections with single command

Posted on

Question :

How do I restore multiple collections at once in mongodb.

I have tried

mongorestore -c Role -c UserAccount -c Permission -d movie-app dump/ 

and I got an error file dump is a directory, not a bson file

I can restore single collection at a time I have to specify the bson file like

  mongorestore -c UserAccount -d movie-app dump/movie-app/UserAccount.bson 

I need to know how do I restore multiple collection with one command.

Answer :

Try these parameter to Include or exclude collections

During restoration, you can ignore the unwanted collections.


Dump all the collections

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

My dump folder has 3 collections.

  1. coll1
  2. coll2
  3. coll3

Restoring coll2 and coll3 collections

I want to restore coll2 and coll3 in the test db.

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

Check the collections

> use test
switched to db test
> show collections

For restore collections with similar names

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

Restore collections with different pattern

There is no straight forward method for this, but we can create a loop to restore this.

create a file with list of collections that we need to restore

vi collections
#add the collections names and save.

shell script for restore in loop

while IFS= read -r col_name
  mongorestore --db sqladmin --collection $col_name $backup_path/$col_name.bson
done < "$input"

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

Check the collections

> use sqladmin
switched to db sqladmin
> show collections

How do I restore multiple collections at once in mongodb

if you want to restore multiple collections of dumps at a time from your MongoDB dumps then simply use in mongo shell the below command.

mongorestore --verbose C:locationofdumpsFiles

Note:- You must sure you are running the above command in BIN directory of MongoDB Server.

Suppose that in my environment the MongoDB installation location is such as

C:Program FilesMongoDBServer3.6bin

So, i will first open the ‘command prompt’ and go to the BIN location of the MongoDB. Because mongorestore shell is reside inside of MongoDB BIN foleder.

C:Program FilesMongoDBServer3.6bin>

Then run the command such as

 C:Program FilesMongoDBServer3.6bin>mongorestore --verbose C:locationofdumpsFiles

Leave a Reply

Your email address will not be published. Required fields are marked *