What's the fastest way to copy a collection within the same database?

后端 未结 8 644
余生分开走
余生分开走 2020-11-30 19:01

I want to copy a collection within the same database and give it a different name - basically take a snapshot.

What\'s the best way to do this? Is

相关标签:
8条回答
  • 2020-11-30 19:17

    First option (Using mongo dump)

    1. Get a dump from collection

      mongodump -d db -c source_collection

    2. Restore from collection

      mongorestore -d db -c target_collection dir=dump/db_name/source_collection.bson

    Second Option

    1. Running aggregate

      db.getCollection('source_collection').aggregate([ { $match: {"emailAddress" : "apitester@mailinator.com"} }, { $out: "target_collection" } ])

    Third Option (Slowest)

    1. Running a through for loop

      db.getCollection('source_collection').find().forEach(function(docs){ db.getCollection('target_collection').insert(docs); }) print("Rolleback Completed!");

    0 讨论(0)
  • 2020-11-30 19:19

    Note: Read the answer Updates, they are important!


    The most simple & efficient way is by using copyTo(), so you can use:

    db.source.copyTo("target"); 
    

    & if "target" doesn't exist, it will be created

    -- Update --

    According to CopyTo Documentation, Because copyTo() uses eval internally, the copy operations will block all other operations on the mongod instance. So it shouldn't be used on production environment.

    -- Update --

    Because CopyTo() uses eval() internally & eval() is deprecated since version 3.0, so CopyTo() is also deprecated since version 3.0.

    0 讨论(0)
  • 2020-11-30 19:20

    This is my implementation in python (pymongo):

    def copy_collection(client, from_db, from_coll, to_db=None, to_coll=None):
        to_db = from_db if to_db is None else to_db
        to_coll = from_coll if to_coll is None else to_coll
        assert (to_db != from_db or to_coll != from_coll), "Copy Error: Source and destination can't be same!"
        documents = client[from_db][from_coll].find()
        client[to_db][to_coll].insert_many([d for d in documents])
    
    0 讨论(0)
  • 2020-11-30 19:20

    The fastest way is db.collection.copyTo().

    Note that it is deprecated since version 3.0.

    0 讨论(0)
  • 2020-11-30 19:25

    You can use the copyDatabase function in the mongo shell:

    http://docs.mongodb.org/manual/tutorial/copy-databases-between-instances/

    0 讨论(0)
  • 2020-11-30 19:28

    You have a few options, but the fastest is:

    mongodump -d db -c sourcecollection 
    mongorestore -d db -c targetcollection --dir=dump/<db>/<sourcecollection.bson>
    

    or

    mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop
    

    or in php:

    `mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop`;
    

    after that you have

    mongo db < script.js
    

    where, as shown in the mongo docs, script.js contains something like:

    db.myoriginal.find().forEach( function(x){db.mycopy.insert(x)} );
    

    The slowest (by an order of magnitude or more) way to copy a collection will be to use the native php driver - simply because of moving information around. But you could issue the above mongo query if you absolutely want to avoid cli calls using the db execute function.

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