Restoring single collection in an existing mongodb

MongodbUbuntu 14.04

Mongodb Problem Overview


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!

Mongodb Solutions


Solution 1 - Mongodb

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

Solution 2 - Mongodb

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/

Solution 3 - Mongodb

Steps to restore specific collection in the mongodb.

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

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

    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:

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

Solution 4 - Mongodb

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonRedView Question on Stackoverflow
Solution 1 - MongodbAbhay PSView Answer on Stackoverflow
Solution 2 - MongodbwordsforthewiseView Answer on Stackoverflow
Solution 3 - MongodbKrutarth ChokshiView Answer on Stackoverflow
Solution 4 - MongodbAlexander MillsView Answer on Stackoverflow