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

前端 未结 19 1509
無奈伤痛
無奈伤痛 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.

    0 讨论(0)
  • 2020-11-28 01:06

    At the moment there is no command in MongoDB that would do this. Please note the JIRA ticket with related feature request.

    You could do something like:

    db.<collection_name>.find().forEach(function(d){ db.getSiblingDB('<new_database>')['<collection_name>'].insert(d); });
    

    Please note that with this, the two databases would need to share the same mongod for this to work.

    Besides this, you can do a mongodump of a collection from one database and then mongorestore the collection to the other database.

    0 讨论(0)
  • 2020-11-28 01:06

    If RAM is not an issue using insertMany is way faster than forEach loop.

    var db1 = connect('<ip_1>:<port_1>/<db_name_1>')
    var db2 = connect('<ip_2>:<port_2>/<db_name_2>')
    
    var _list = db1.getCollection('collection_to_copy_from').find({})
    db2.collection_to_copy_to.insertMany(_list.toArray())
    
    0 讨论(0)
  • 2020-11-28 01:07

    You can use aggregation framework to resolve your issue

    db.oldCollection.aggregate([{$out : "newCollection"}])
    

    It shoul be noted, that indexes from oldCollection will not copied in newCollection.

    0 讨论(0)
  • 2020-11-28 01:07

    This can be done using Mongo's db.copyDatabase method:

    db.copyDatabase(fromdb, todb, fromhost, username, password)
    

    Reference: http://docs.mongodb.org/manual/reference/method/db.copyDatabase/

    0 讨论(0)
  • 2020-11-28 01:09

    I would abuse the connect function in mongo cli mongo doc. so that means you can start one or more connection. if you want to copy customer collection from test to test2 in same server. first you start mongo shell

    use test
    var db2 = connect('localhost:27017/test2')
    

    do a normal find and copy the first 20 record to test2.

    db.customer.find().limit(20).forEach(function(p) { db2.customer.insert(p); });
    

    or filter by some criteria

    db.customer.find({"active": 1}).forEach(function(p) { db2.customer.insert(p); });
    

    just change the localhost to IP or hostname to connect to remote server. I use this to copy test data to a test database for testing.

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