Mongodb monogorestore "root directory must be a dump of a single database" - mongodb

I'm trying to restore a mongodump to a differently named database (which should be possible via --db <dbname> switch).
My working directory contains a single dump folder, which contains a single db dump.
However, when I try this command:
mongorestore --port xxxxx --db some_destination_db
I get the following error:
ERROR: ERROR: root directory must be a dump of a single database
ERROR: when specifying a db name with --db
I have no idea why I'm getting this, and can't find any help on google. Anyone have any ideas?

You need to include the path of the source db dump directory to restore from (as at MongoDB 2.6).
For example, if restoring from "twitter" to "some_destination_db" your command line would be similar to:
mongorestore --port xxxxx --db some_destination_db dump/twitter

May be your destination path contains some subfolders

Related

Why mongorestore always returning the error that directory not found even with correct file path?

I want to import some data in mongoDb database's collection. That collection already have some data and i want to import some more data into it. So for that i am using this command
mongorestore --drop -d local-db -c users /Documents/cwc/mongo-backups/Users.bson
but this command is always returning this error
Failed: mongorestore target '/Documents/cwc/mongo-backups/Users.bson' invalid: stat /Documents/cwc/mongo-backups/Users.bson: no such file or directory
How can i fix this?

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)

mongodump with different folder name

Is there a way to dump mongo database to a specific folder name?
Lets say I have a db named myDb
When I use:
mongodump --db myDb
It generates a dump folder with a myDb folder inside it
Is it possible to get something like dump/mySpecifiedFolder from mongodump?
You cannot change the name of the myDB folder, since that's named for the database which it contains, but you can change the location of the myDb folder. To do this, use the --out / -o parameter.
From the docs:
--out , -o
Specifies the directory where mongodump will write BSON files for the dumped databases. By default, mongodump saves output files in a directory named dump in the current working directory.
So, this command ...
mongodump --db myDb -o /some/path/
... would cause mongodump to create /some/path/myDb.
In my discovery of doing a mongodump, please note that the default location can be the Desktop when outputting.
Example
"C:\Program Files\MongoDB\Server\3.4\bin\mongodump.exe" --host localhost --port 27017 --db local --out dump/dpusers
On your Desktop, the dump folder will be created with the subfolder "dpusers" and inside that folder is another folder named the same as your database name.

Cannot import example dataset (the system cannot find the specified file)

I am following the example given on MongoDB's website here, but I am running into trouble when trying to import sample data.
When running the command
mongoimport --db test --collection restaurants --drop --file primer-dataset.json
I get the error:
Failed: open primer-dataset.json: The system cannot find the file specified
The problem is, I am not sure what directory MongoDB expects this file to be in. I tried placing it in data/db, but that did not work. Note that I am only using default settings.
I know this is a somewhat trivial question and I feel stupid for asking it but I can not find documentation on this anywhere. Where is MongoDB expecting import files?
MongoDB expects the file to be in the directory from where you are running the command mongoimport.
If you place your file under data/db then set mongodb path as global environment variable and execute the command from data/db directory.
Additionally if you have security enabled for your mongodb then you need to execute command as below
mongoimport --username admin --password password --db test --collection restaurants --drop --file primer-dataset.json
here admin is the user authorized to perform db operations for test database and restaurants is the collection name.
For Windows!
Save file using notepad++ in .json format at MongoDB/bin where mongoimport command is present.
Simple notepad has trouble doing it.
It happened to me as well. The issue was that though the file was visible as restaurants.json actually the file was restaurants.json.json (since saved in JSON format). The issue was resolved after properly changing the name.
i have trouble like you, check you path to file, mongoimport.exe and your file may be stay in another folders.
use mongoimport -d test1 -c restaraunts companies.json for import to mongodb.
Check the filename extension, and make sure it's a ".json" file;
After this I successfully run the
mongoimport --db test --collection restaurants --drop --file [path\to\Json file]
command;
In my case, I removed the --drop parameter and it worked perfectly. I guess, it is throwing this error:
Failed: open paht/file-name.json: The system cannot find the file specified.
because the collection it wants to drop is not available, because I have not created any before.
you must copy your json file into C:\Windows\System32 and write this command on cmd:
mongoimport --db test --collection mongotest --type json --file yournamefile.json

mongorestore syntax error

I have lots of bson files in the following path:
c:/mongodb/bin/dump/Sid
If I run the command:
> mongorestore --db Sid --drop dump/Sid
I get the following error:
Mon Mar 26 14:36:36 SyntaxError: missing ; before statement (shell):1
What is the problem with my command?
From your input, it appears as though you are attempting to run mongorestore from inside the JS shell.
Mongorestore is a standalone application, and is run directly from the terminal.
The following will not work:
c:\mongodb-win32-x86_64-2012-03-20\bin>mongo.exe
MongoDB shell version: 2.1.1-pre-
connecting to: test
> mongorestore --db test --drop \dump\test
Mon Mar 26 11:29:13 SyntaxError: missing ; before statement (shell):1
>
If you run mongorestore directly from the terminal you should be successful:
c:\mongodb-win32-x86_64-2012-03-20\bin>mongorestore --db test --drop \dump\test
connected to: 127.0.0.1
... (truncated for brevity) ...
c:\mongodb-win32-x86_64-2012-03-20\bin>
The documentation on Mongodump / mongorestore may be found in the "Import Export Tools" documentation:
http://www.mongodb.org/display/DOCS/Import+Export+Tools
mongorestore is not a command , it an executable in the bin directory of MongoDB.
Below is the quote from http://docs.mongodb.org/manual/reference/program/mongorestore/
The mongorestore program writes data from a binary database dump
created by mongodump to a MongoDB instance. mongorestore can create a
new database or add data to an existing database.
If you already have a mongod instance running
where you already specified the dbpath as
mongod --dbpath "..\mongodb\data"
you can directly run the mongorestore command .
mongorestore ..\data\dump
You can't use mongorestore command like this...
You have to run this via cmd and not on Mongo Shell... Have a look at below command...
>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson
Here path\to\mongorestore.exe is path of mongorestore.exe inside bin folder of mongodb.
dbname is name of databse. collection_name is name of collection.bson.
path/to/same/collection.bson is the path up to that collection.
Now from mongo shell you can verify that database is created or not (If it does not exist, database with same name will be created with collection).
If you want to restore the outside database then copy that database in
<pre>C:\database drive(Create a folder database and copy your database) ,then follow the steps
1)c:\> cd database
2)c:\database>dir
3)c:\database>"\Program Files\MongoDB\Server\3.0\bin\mongorestore.exe"
now open robomongo and check it will contain the restored dbs.. or check on command prompt show dbs</pre>