How to use mongodump for 1 collection

后端 未结 4 768
小蘑菇
小蘑菇 2021-01-30 00:58

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

How should I use the command and its options?

相关标签:
4条回答
  • 2021-01-30 01:14

    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
    
    0 讨论(0)
  • 2021-01-30 01:15

    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.

    0 讨论(0)
  • 2021-01-30 01:21

    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

    0 讨论(0)
  • 2021-01-30 01:23

    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.

    0 讨论(0)
提交回复
热议问题