How to use mongodump for 1 collection

别等时光非礼了梦想. 提交于 2019-12-03 00:37:26

问题


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

How should I use the command and its options?


回答1:


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.




回答2:


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




回答3:


Very basic commands for dump mongodb.

  1. Dump all collection

    mongodump
    
  2. Dump specific database only

    mongodump --db=DB_NAME
    
  3. Dump database with username & password

    mongodump -u=USERNAME -p=PASSWORD --db=DB_NAME
    
  4. 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.



来源:https://stackoverflow.com/questions/16347134/how-to-use-mongodump-for-1-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!