I am using MongoDB on Openshift for my application. But i'm not able to locate database files. And if i use mongoexport command where those files will be saved (i'm aware about --out option). Where is mongoexport util exists? And i'm not able to successfully run mongoexport on openshift.
Can anyone help me on this?
First go to your repo folder.
cd app-root/repo/
Then run below mongoexport command.
mongoexport --host hostname --port port --authenticationDatabase admin --username admin --password password --db test --collection collection --out collection.json
--authenticationDatabase is must option when you specify username and password that exists in different database.
Refer this documentation for more options.
Related
I am trying to export my local mongodb data to an atlas cluster and i created a dump and now i am using the command,
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-qwo7v.mongodb.net:27017,cluster0-shard-00-01-qwo7v.mongodb.net:27017,cluster0-shard-00-02-qwo7v.mongodb.net:27017 --ssl --username username --password <PASSWORD> --authenticationDatabase admin
to try to restore it but is giving me the system cannot find the file specified.
But if i type mongorestore then it works but it doesn't restore to the atlas cloud server whatever.
What am i doing wrong?
Edit: The path i have used is C:\Program Files\MongoDB\Server\4.2\bin\dump\gfg and dump\gfg but it is still not working.
Usage of MongoRestore.exe can be found here.
Standalone MongoDB
When you use mongorestore, you need to provide the location of the file/dump you need to restore
mongorestore --host=mongodb1.example.net --port=27017 --username=user --authenticationDatabase=admin /opt/backup/mongodump-2011-10-24
last portion of the command is missing that defines the location of the data that needs to be restored.
ReplicaSet
To Restore MongoDB on a Replica, You have to stop the mongod and replace the files for mongodb. Please read here for restoring database on replica set.
For restore from standalone to replica,
mongorestore --host myReplSet/mongo0.example.com:27020,mongo1.example.com:27012 --db <dbname> <folder_location>
If this still does not work, check path and ensure its properly escaped (spaces in name) or quoted.
Your code with quotes and path:
mongorestore --host Cluster0-shard-0/cluster0-shard-00-00-qwo7v.mongodb.net:27017,cluster0-shard-00-01-qwo7v.mongodb.net:27017,cluster0-shard-00-02-qwo7v.mongodb.net:27017 --ssl --username username --password <PASSWORD> --authenticationDatabase admin "C:\Program Files\MongoDB\Server\4.2\bin\dump\gfg"
Is there a way to export the whole database in mongodb instead of exporting a collection this way?
{"_id":{"$oid":"5d3de201b128f8eccc1979a5"},"user":"myuser","password":"$2y$10$euzVCeHJ4XAT0xQuQzUotenktVGCQ5darCSWWQtfYE80IqLovDNfi","widgets":[{"name":"w1","color":"blue"},{"name":"w2","color":"green"}]}
mongodump is a way to do this.
See the documentation
yes please use mongodump
Some parameters are as follow
--db <database>, -d <database>
If you do not specify a database, mongodump copies all databases in this instance into the dump files.
--collection <collection>, -c <collection>
If you do not specify a collection, this option copies all collections in the specified database or instance to the dump files.
so with your condition you can do it like this
mongodump --host mongodb1.example.net --port 37017 --username user --password "pass" --db yourdatabasename --out /opt/backup/mongodumpdir
the default host port is localhost & 27017 .
If you haven't changed the default, you can ignore this
I restored MongoDB production database to our testing environment using the mongodump and mongorestore commands. There were 414 user defined functions in our database and none of them were restored. How can I restore the functions in the production environment in the testing environment?
It is pretty simple. The system.js function is also a collection. So you can dump the collection using the following command and restore using the mongorestore command.
mongodump --host youripaddressorlocal --port yourportnumber --username "username" --password "password" --authenticationDatabase admin --collection system.js --db databasename
mongorestore --host localhost --port 27017 --username "username" --password "yourpassword" --collection system.js --drop --db yourdatabase dump/baabtra_db/system.js.bson
--drop is used so that if the function is already there, it will be deleted. Incase if you are still getting the error and if you are not able to delete it, you can use the following command to delete all the functions in the mongoDb.
db.system.js.remove({})
Please note that the curly braces are very important.
Just finished setting up my mongodb and found out that there is a free service called Atlas. Started up a cluster and ran a mongodump and mongorestore as explained here https://www.mongodb.com/blog/post/atlas-on-day-one-importing-data, but can't seem to get it to work.
Here is my shell commands:
mongorestore --ssl --db=infovis --host infovis-shard-00-00-nmctc.mongodb.net:27017,infovis-shard-00-01-nmctc.mongodb.net:27017,infovis-shard-00-02-nmctc.mongodb.net:27017/test?replicaSet=Infovis-shard-0" --authenticationDatabase admin --dir=dump/infovis --username danielbook --password <Password>
What am I doing wrong?
EDIT: Solved the problem by running mongoimport on the server instead.
So, I solved this by using mongoimport instead. I had just created the database in mongo, so I could just use the same csv files and then use
mongoimport -h cluster0-shard-00-00-nmctc.mongodb.net:27017 -d infovis -c flights -u <USER> -p <PASSWORD> --file march_2016.csv --type csv --headerline
for each file I want to import to the Atlas database.
How can I use mongodump to move a single collection from one database to another?
How should I use the command and its options?
I think it's just:
mongodump --db=<old_db_name> --collection=<collection_name> --out=data/
mongorestore --db=<new_db_name> --collection=<collection_name> data/<db_name>/<collection_name>.bson
Also see docs here and here.
Btw, the other way to move the collection from one database to another is to use renameCollection:
db.runCommand({renameCollection:"<old_db_name>.<collection_name>",to:"<new_db_name>.<collection_name>"})
Here's some related SO threads:
How to copy a collection from one database to another in MongoDB
How to use the dumped data by mongodump?
Taking database (document) dump (backup)
mongodump --host <hostname-of-mongoserver> --db <db-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
Taking collection dump (backup)
mongodump --host <hostname-of-mongoserver> --db <db-name> --collection <collection-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
mongodump documentation
Very basic commands for dump mongodb.
Dump all collection
mongodump
Dump specific database only
mongodump --db=DB_NAME
Dump database with username & password
mongodump -u=USERNAME -p=PASSWORD --db=DB_NAME
Dump from another host
mongodump --host HOST_NAME/HOST_IP --port HOST_PORT --out {YOUR_DIRECTOTY_PATH} --db=DB_NAME
Only able to dump from another host when they allow it.
If it's a replica set and you want to use the --uri you should use it like this cause documentation states that you can't specify some options when using --uri
mongodump --uri "mongodb://user:password#mongo-en-1.example.io:27017,mongo-en-2.example.io:27017,mongo-en-3.example.io:27017/$Databasename?replicaSet=$replicasetname&authSource=admin" --collection $collectionname
Then restore it the usual way.
Here is an example of how you can export a single collection with mongodump.exe on Windows 10:
"D:\Program Files\MongoDB\Server\4.0\bin\mongodump.exe" -h localhost --port 27017 -d meteor --collection users -o meteor_users
The exported collection is users, the database is meteor, the host localhost, the port is 27017.
The output will be stored in directory meteor_users.
Restoring should use a command like this one:
"D:\Program Files\MongoDB\Server\4.0\bin\mongorestore.exe" -d meteor -c users users.bson
None of them works for me while doing dump for MongoDB atlas. Here is the minor change in the host that work for me
Dump
mongodump --uri mongodb+srv://<USERNAME>:<PASSWORD>#host.abcd.mongodb.net/db_name --collection "user_collection" --gzip --out db_backup_folder
Restore
mongorestore --uri mongodb+srv://<USERNAME>:<PASSWORD>#dbhost.abcd.mongodb.net -d db_name --gzip db_backup_folder
atlas-database-tools-backup-restore