MongoDB - dump and restore across different host, db with oplog - mongodb

Is it possible to take a mongodump and mongorestore it to a different hosts, with different DB names, with oplog enabled?
From: mongodb://user:password#source-hostname:source-port/db1
To: mongodb://user:password#dest-hostname:dest-port/db5
When I do a mongodump with oplog on the source MongoDB, it takes a dump of the entire DB.
mongodump --oplog --host <source-hostname> -u <user> -p <password> --port <source-port> --authenticationDatabase admin
Now for the restore, I want to restore to a different hostname, and the db-name is also different. Is there way to restore the data to this db, with the oplogReplay?
mongorestore --host <dest-host> --port <dest-port> --username <user> --password <password> --authenticationDatabase admin --oplogReplay --db <db5> <path-to-dump>/dump
If I use oplogReplay, I am getting the following error
Can only replay oplog on full restore
I do not want to do a full restore, as it will create the db-name as db1, whereas I want to make use of db5. Also, there are already multiple DBs on this destination host and I do not want to bombard with another new database.
Any suggestions on this issue?

You can't use two options --oplogReplay and --db at the same time.
If you don't want to restore the full DB, simply go to dump/ folder and delete files for DBs other than db5. Then retry mongorestore without --db:
mongorestore --host <dest-host> --port <dest-port> --username <user> --password <password> --authenticationDatabase admin --oplogReplay <path-to-dump>/dump
If this doesn't not work for you, you may need to import oplog collection to a temporary db, and manipulate it to remove all except records for db5.

Related

MongoRestore is giving me the system cannot find the file

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"

Export the whole database on mongodb

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

How to restore user defined javascript functions in MongoDB?

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.

coping mongo collection fails to maintain insertion order

The following command needs to copy a previously "dumped" mongo database from local machine to Mongodb cluster "Atlas", which it does but it fails to maintain the order of documents.
mongorestore --maintainInsertionOrder --ssl --db=myDB --host cluster0-shard-00-00-oko1k.mongodb.net:27017,cluster0-shard-00-01-oko1k.mongodb.net:27017,cluster0-shard-00-02-oko1k.mongodb.net:27017 --authenticationDatabase admin --dir=dump/myDB -u myname --password mypassowrd --drop
Any idea what is the fix so that the order of the collection at the destination is maintained?

How to use mongodump for 1 collection

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