How to restore gzipped Monogdb single collection? - mongodb

Hye everyone, I am new to mongodb I dumped one of my collection using following command mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz given on the link now when I try to restore it using mongorestore it gives Error dont know what to do with file.
kindly help me I used this way because the answer in the given link got too much upvotes
Error image

You can just add gzip to the restore command:
mongorestore --gzip --db projectx userprofiles/

Okay I have got my answer first of all I converted the extension of my gunzipped file to bson by using command
gunzip -c userprofiles.gz > users.bson
after that I used mongorestore command
mongorestore --db projectx userprofiles/
Thats it

Related

Can't import via mongorestore

I am an absolute beginner in mongoDB. I am trying to import the sample data using mongorestore by doing the following:
C:\Users\hp\Downloads\mongodb-database-tools-windows-x86_64-100.3.1\mongodb-database-tools-windows-x86_64-100.3.1\bin>mongorestore --nsInclude=exampleDb C:\Users\hp\Desktop\aqa-test-tools\TestResultSummaryService\data\sampleData.gz
But I am getting the following error continuously:
2021-07-15T20:24:07.485+0530 Failed: file C:\Users\hp\Desktop\aqa-test-tools\TestResultSummaryService\data\sampleData.gz does not have .bson extension
2021-07-15T20:24:07.487+0530 0 document(s) restored successfully. 0 document(s) failed to restore.
Then I also came to know that my file is unzipped, so for that I also tried doing:
mongorestore --gzip --nsInclude='exampleDb.*' C:\Users\hp\Desktop\aqa-test-tools\TestResultSummaryService\data\sampleData.gz
But the output remains the same.
I am using the mongoDB version 4.4.6
Can anyone tell me where and what am I doing wrong?
Thank You
It looks like you are trying to restore a zipped file as for the path you have given to mongorestore tool:
C:\Users\hp\Desktop\aqa-test-tools\TestResultSummaryService\data\sampleData.gz
You can add the flag --gzip to sort the problem out.
mongorestore --gzip ...
As an alternative you can unzip the file on your machine and restore the output.
In older versions of MongoDB, we can use mongorestore like this:
mongorestore --gzip --archive="fullpath to your file" --db "db name"
In newer versions, as --db is deprecated, we can use mongorestore in this way:
mongorestore --gzip --archive="fullpath to your file" "db name"
In your case you should probably enter this command:
mongorestore --gzip --archive=C:\Users\hp\Desktop\aqa-test-tools\TestResultSummaryService\data\sampleData.gz exampleDb

mongorestore file X does not have .bson extension

I'm trying to run mongorestore through docker to restore the database to another dockerized mongo on the system:
sudo docker run --net=host -v $PWD:/home/mongo mongo /bin/bash -c "mongorestore -d venko /home/mongo/mongo_venko_20200326230306.archive"
but I get
2020-03-27T00:17:32.645+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
2020-03-27T00:17:32.645+0000 Failed: file /home/mongo/mongo_venko_20200326230306.archive does not have .bson extension
2020-03-27T00:17:32.645+0000 0 document(s) restored successfully. 0 document(s) failed to restore.
Answers from mongorestore error: Don't know what to do with the dump file tell me to pass the -db option but I did pass so I don't know what to do.
I have to use both options --gzip and --archive
mongorestore --uri="uri" --gzip --archive=/Path/to/archive/abc.gz
As the error mentions the mongorestore looks for a BSON file, while the archive is not the extension it is looking for.
You can do the following:
Either use:
mongorestore --gzip /home/mongo/mongo_venko_20200326230306.archive
Or, Extract the archive file and use:
mongorestore /home/mongo/mongo_venko_20200326230306/<filename.bson>

MongoDb: How to import dump data from .gz file?

I want to import dump data from my .gz file.
Location of file is home/Alex/Documents/Abc/dump.gz and the name of db is "Alex".
I have tried mongorestore --gzip --db "Alex" /home/Alex/Documents/Abc/dump.gz
But it shows error:
2018-10-31T12:54:58.359+0530 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
2018-10-31T12:54:58.359+0530 Failed: file
/home/Alex/Documents/Abc/dump.gz does not have .bson extension.
How can I import it?
Dump command:
mongodump --host localhost:27017 --gzip --db Alex --out ./testSO
Restore Command:
mongorestore --host localhost:27017 --gzip --db Alex ./testSO/Alex
Works perfectly!
While using archive:
Dump command:
mongodump --host localhost:27017 --archive=dump.gz --gzip --db Alex
Restore Command:
mongorestore --host localhost:27017 --gzip --archive=dump.gz --db Alex
Note:- While using archive you need to stick with the database name.
Different database name or collection name is not supported. For more info.
This is what worked for me in the latest versions (100.5.1) of mongodump.
mongorestore --uri=<CONNECTION_URI> --gzip --archive=<ARCHIVE_NAME> --nsFrom "<SOURCE_DB_NAME>.*" --nsTo "<DEST_DB_NAME>.*"
Unpack .tgz files and restore the DB
tar zxvf fileNameHere.tgz
mongorestore --port 27017 -u="username" -p="password" --authenticationDatabase admin /bacup_path
mongorestore doesn't find the BSON files inside the gzip file because the mongodump was made with different paths than the restore one.
To solve the problem, the fastest and safest way is to extract the gzip file and go to the upper folder containing the json and bson files for run the mongorestore.
For example, the dump.gz file was made in such a way that the backup are saved within the data/backup/mongo/dump/ path folders
Extracting the dump.gz file with command tar -xvf dump.gz you will find a folder named data with the subfolders data/backup/mongo/dump/ inside (inside the dump folder are present all backup file with json and bson extension, these files represent databases and collections, etc.)
Go to the higher folder, that containing the dump folder eg. cd data/backup/mongo/
Now you can run the restore command
mongorestore --authenticationDatabase admin dump/
Where dump/ is the folder that containing the backup files.
You may need to use the arguments -h to point the server host (eg. localhost) and -u followed by the username enabled to make the restore operations (eg. root)

Mongorestore from stdin

Has someone ever managed to restore a mongodump from stdin?
I generated the backup file using the referenced command:
mongodump --archive > file
But the reference never explains in what format the archive is, or how to restore it. Some people say you have to inject it from stdin, but they remains a mystery as there is no evidence that it is actually possible.
As a matter of fact, I did the following tries, without any success:
cat file | mongorestore
mongorestore -vvvvv --archive < file
mongorestore -vvvvv --archive=file
All these commands end up with the same error:
Failed: stream or file does not appear to be a mongodump archive
For information, I managed to restore an archive that was generated the classic way.
Why do I need to use the --archive to stdout?
My mongodb is inside a docker.
The option I'm using now creates the backup file inside the docker, that I then copy to the host, using the docker cp command, so I use twice the space needed (before it's removed inside the container).
Unfortunately, the docker has no mount from the host, and I cannot (yet) restart it to add the option. I was looking for a quick option.
So according to MongoDB official mongodump doc and mongorestore doc:
To output the dump to the standard output stream in order to pipe to another process, run mongodump with the archive option but omit the filename.
To restore from the standard input, run mongorestore with the --archive option but omit the filename.
So actually you don't have to mongodump to file first and then read from it, just simply pipe mongodump and mongorestore like this:
mongodump --archive | mongorestore --archive --drop
However, you may probably get another problem as I did https://stackoverflow.com/a/56550768/3785901.
In my case, I have to use --nsFrom and --nsTo instead of --db, or the mongorestore didn't work as expected. So the final command I successfully execute to mongodump/mongorestore is:
mongodump --host HOST:PORT --db SOURCE_DB --username USERNAME --password PASSWORD --archive | mongorestore --host HOST:PORT --nsFrom 'SOURCE_DB.*' --nsTo 'TARGET_DB.*' --username USERNAME --password PASSWORD --archive --drop
Good luck.

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!
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
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/
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
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