Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 404
user2325703 Avatar asked May 02 '13 20:05

user2325703


People also ask

Where is Mongodump used?

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.

What is the difference between Mongoexport and Mongodump?

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.


2 Answers

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?

Hope that helps.

like image 187
alecxe Avatar answered Sep 24 '22 02:09

alecxe


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

like image 25
Vaseem007 Avatar answered Sep 24 '22 02:09

Vaseem007