I want to export all collections in MongoDB by the command:
mongoexport -d dbname -o Mongo.json
The result is:
No collection specifie
If you want to dump all collections in all databases (which is an expansive interpretation of the original questioner's intent) then use
mongodump
All the databases and collections will be created in a directory called 'dump' in the 'current' location
If you want to backup all the dbs on the server, without having the worry about that the dbs are called, use the following shell script:
#!/bin/sh
md=`which mongodump`
pidof=`which pidof`
mdi=`$pidof mongod`
dir='/var/backup/mongo'
if [ ! -z "$mdi" ]
then
if [ ! -d "$dir" ]
then
mkdir -p $dir
fi
$md --out $dir >/dev/null 2>&1
fi
This uses the mongodump utility, which will backup all DBs if none is specified.
You can put this in your cronjob, and it will only run if the mongod process is running. It will also create the backup directory if none exists.
Each DB backup is written to an individual directory, so you can restore individual DBs from the global dump.
For lazy people, use mongodump
, it's faster:
mongodump -d <database_name> -o <directory_backup>
And to "restore/import" it (from directory_backup/dump/):
mongorestore -d <database_name> <directory_backup>
This way, you don't need to deal with all collections individually. Just specify the database.
Note that I would recommend against using mongodump
/mongorestore
for big data storages. It is very slow and once you get past 10/20GB of data it can take hours to restore.
First, of Start the Mongo DB - for that go to the path as ->
C:\Program Files\MongoDB\Server\3.2\bin and click on the mongod.exe file to start MongoDB server.
Command in Windows to Export
C:> mongodump --host remote_ip_address:27017 --db -o C:/Users/Desktop/temp-folder
Command in Windows to Import
C:> mongorestore --host=ip --port=27017 -d C:/Users/Desktop/temp-folder/db-dir
To export in JSON format do this by following commands which you can see.
mongoexport --db dbname --collection collectionName --out directoryPATH/JSONfileName.json
Exporting all collections using mongodump use the following command
mongodump -d database_name -o directory_to_store_dumps
To restore use this command
mongorestore -d database_name directory_backup_where_mongodb_tobe_restored