mongo copy from one collection to another (on the same db)

后端 未结 7 540
暖寄归人
暖寄归人 2020-12-24 08:02

I have got mongo db called test and in this db two collections collection1 and collection1_backup. How to replace content of col

相关标签:
7条回答
  • 2020-12-24 08:16

    simply just do this.

    //drop collection1

    db.collection1.drop();
    

    //copy data from collection1_backup to collection1

    db.collection1.insert(db.collection1_backup.find({},{_id:0}).toArray());
    
    0 讨论(0)
  • 2020-12-24 08:19

    Using Java Driver

    Try below one:

    public void copyTo(String db,String sourceCollection,String destinationCollection,int limit) throws        
    UnknownHostException {
    
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB database = mongo.getDB(db);
        DBCollection collection = database.getCollection(sourceCollection);
        DBCursor dbCursor = collection.find().limit(limit);
        List<DBObject> list =  dbCursor.toArray();
        DBCollection destination =  database.getCollection(destinationCollection);
        destination.insert(list, WriteConcern.NORMAL); //WRITE CONCERN is based on your requirment.
    
    }
    
    0 讨论(0)
  • 2020-12-24 08:24

    You can use a simple command to Backup MongoDB Collection. It will work only on MongoDB 4.0 or earlier versions.

    db.sourceCollectionName.copyTo('targetCollectionName')
    

    Your targetCollectionName must be in Single(') or Double(") Quote

    Note:

    The db.collection.copyTo() method uses the eval command internally. As a result, the db.collection.copyTo() operation takes a global lock that blocks all other read and write operations until the db.collection.copyTo() completes.

    0 讨论(0)
  • 2020-12-24 08:25

    also usefull: to export collection to json file

    mongoexport --collection collection1_backup --out collection1.json
    

    to import collection from json file

    mongoimport --db test --collection collection1 --file collection1.json
    

    to import single collection from backup/dump file one need to convert *.bson file to *.json by using

    bsondump collection1_backup.bson > collection1_backup.json
    
    0 讨论(0)
  • 2020-12-24 08:28

    This can be done using simple command:

    db.collection1_backup.aggregate([ { $match: {} }, { $out: "collection1" } ])
    

    This command will remove all the documents of collection1 and then make a clone of collection1_backup in collection1.

    Generic Command would be

    db.<SOURCE_COLLECTION>.aggregate([ { $match: {} }, { $out: "<TARGET_COLLECTION>" } ])
    

    If TARGET_COLLECTION does not exist, the above command will create it.

    0 讨论(0)
  • 2020-12-24 08:35

    The best way to have done this (considering the name of the collection ends with _backup) is possibly to have used mongorestore: http://docs.mongodb.org/manual/reference/mongorestore/

    However in this case it depends. If the collection is unsharded you can use renameCollection ( http://docs.mongodb.org/manual/reference/command/renameCollection/ ) or you can use a more manual method of (in JavaScript code):

    db.collection1.drop(); // Drop entire other collection
    db.collection1_backup.find().forEach(function(doc){
       db.collection1.insert(doc); // start to replace
    });
    

    Those are the most common methods of doing this.

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