Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring single collection in an existing mongodb

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.

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!

like image 811
JonRed Avatar asked Feb 21 '15 00:02

JonRed


People also ask

How do I restore a specific database in MongoDB?

The basic way to restore a database is to use the mongorestore command to specify the backup directory (dump directory) without any options. This option is suitable for databases located in the localhost (127.0. 0.1) using the port 27017.

How do I copy a collection from one MongoDB to another?

@Naman what is the use case of copy collection, i mean you need any command or it is ok with manually process? for the manual process just install studio3T connect both databases and right click on collection that you want to copy, click on option "Copy Collection" and then go to second database right click on " ...


2 Answers

I think this is now done with the --nsInclude option:

mongorestore --nsInclude test.purchaseorders dump/

dump/ is the folder with your mongodump data, test is the db, and purchaseorders is the collection.

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

like image 40
wordsforthewise Avatar answered Sep 21 '22 04:09

wordsforthewise


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 -

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson 
like image 154
Abhay PS Avatar answered Sep 22 '22 04:09

Abhay PS