How can I use mongodump
to move a single collection from one database to another?
How should I use the command and its options?
The mongodump is a utility for creating database backups. The utility can be used for creating binary export of the database contents. Mongodump can export data from both mongod and mongos instances, allowing you to create backups from: A standalone, replica set.
mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.
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:
Hope that helps.
mongodump --host <hostname-of-mongoserver> --db <db-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
mongodump --host <hostname-of-mongoserver> --db <db-name> --collection <collection-name> --username <dbuser-name> --password <password> --gzip --out </backup/location/>
mongodump
documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With