How to copy a collection from one database to another in MongoDB

前端 未结 19 1578
無奈伤痛
無奈伤痛 2020-11-28 00:22

Is there a simple way to do this?

19条回答
  •  借酒劲吻你
    2020-11-28 01:05

    The best way is to do a mongodump then mongorestore. You can select the collection via:

    mongodump -d some_database -c some_collection
    

    [Optionally, zip the dump (zip some_database.zip some_database/* -r) and scp it elsewhere]

    Then restore it:

    mongorestore -d some_other_db -c some_or_other_collection dump/some_collection.bson
    

    Existing data in some_or_other_collection will be preserved. That way you can "append" a collection from one database to another.

    Prior to version 2.4.3, you will also need to add back your indexes after you copy over your data. Starting with 2.4.3, this process is automatic, and you can disable it with --noIndexRestore.

提交回复
热议问题