mongorestore syntax error - mongodb

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>

Related

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)

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.

Using mongodump to create a single file for each mongo database

I am attempting to create a single archive file for each database in Mongo. Is there a way to create a batch file that will automatically create an archive (.archive) for each database without me manually entering the database name?
The snipet provided
mongodump --host hostename --port portname --username username --password password --authenticationDatabase admin --db databasename --archive=name.arvhive
I would like to create the archive file without having to specify the database name. Is this possible? If not, is there a way to dump all of the databases and create a single gzip for each database?
Any assistance would be greatly appreciated.
Thanks!
In order to get the DB names in the mongo instance we need to use the mongo show dbs command.
Since we want to be able to use this inside a script, we want to run that command and exit the mongo cli, to do that we can pass a js file to mongo, like so:
$ echo "show dbs" > showDbs.js
$ mongo < showDbs.js
Which will result in something like
MongoDB shell version v3.4.22
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.22
admin 0.000GB
animals 0.366GB
people 0.012GB
local 0.000GB
bye
Since we need to parse the result, we can use the --quiet option to reduce the output. We can also skip creating the showDbs.js file so that we don't have to clean it up later.
Now we have:
$ mongo --quiet <<< "show dbs"
admin 0.000GB
animals 0.366GB
people 0.012GB
local 0.000GB
All that's left to do is to parse the result to remove the db sizes, and then run the mongodump command for each of the databases (I'm leaving that command as it is)
To remove the size information for each database we'll use sed and match for a whitespace, since whitespaces are not allowed in database names
$ sed 's/\s\+.\+//' <<< "$dbs"
Putting it all together we get the following script
#!/bin/bash
dbs=$(mongo --quiet <<< "show dbs")
dbs=$(sed 's/\s\+.\+//' <<< "$dbs")
for db in $dbs; do
mongodump --host hostname --port portname --username username --password password --authenticationDatabase admin --db $db --archive=$db.archive
done

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

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