How to use mongodump for 1 collection

user2325703 picture user2325703 · May 2, 2013 · Viewed 77.3k times · Source

How can I use mongodump to move a single collection from one database to another?

How should I use the command and its options?

Answer

alecxe picture alecxe · May 3, 2013

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.