mongodb move documents from one collection to another collection

前端 未结 15 1523
感情败类
感情败类 2020-11-30 22:10

How can documents be moved from one collection to another collection in MongoDB?? For example: I have lot of documents in

15条回答
  •  一向
    一向 (楼主)
    2020-11-30 22:56

    I do like the response from @markus-w-mahlberg, however at times, I have seen the need to keep it a bit simpler for people. As such I have a couple of functions that are below. You could naturally wrap thing here with bulk operators as he did, but this code works with new and old Mongo systems equally.

    function parseNS(ns){
        //Expects we are forcing people to not violate the rules and not doing "foodb.foocollection.month.day.year" if they do they need to use an array.
        if (ns instanceof Array){
            database =  ns[0];
            collection = ns[1];
        }
        else{
            tNS =  ns.split(".");
            if (tNS.length > 2){
                print('ERROR: NS had more than 1 period in it, please pass as an [ "dbname","coll.name.with.dots"] !');
                return false;
            }
            database = tNS[0];
            collection = tNS[1];
        }
        return {database: database,collection: collection};
    }
    
    function insertFromCollection( sourceNS,  destNS, query, batchSize, pauseMS){
        //Parse and check namespaces
        srcNS = parseNS(sourceNS);
        destNS = parseNS(destNS);
        if ( srcNS == false ||  destNS == false){return false;}
    
        batchBucket = new Array();
        totalToProcess = db.getDB(srcNS.database).getCollection(srcNS.collection).find(query,{_id:1}).count();
        currentCount = 0;
        print("Processed "+currentCount+"/"+totalToProcess+"...");
        db.getDB(srcNS.database).getCollection(srcNS.collection).find(query).addOption(DBQuery.Option.noTimeout).forEach(function(doc){
            batchBucket.push(doc);
            if ( batchBucket.length > batchSize){
                db.getDB(destNS.database).getCollection(destNS.collection)insert(batchBucket);
                currentCount += batchBucket.length;
                batchBucket = [];
                sleep (pauseMS);
                print("Processed "+currentCount+"/"+totalToProcess+"...");       
            }
        }
        print("Completed");
    }
    
    /** Example Usage:
            insertFromCollection("foo.bar","foo2.bar",{"type":"archive"},1000,20);    
    

    You could obviously add a db.getSiblingDB(srcNS.database).getCollection(srcNS.collection).remove(query,true) If you wanted to also remove the records after they are copied to the new location. The code can easily be built like that to make it restartable.

提交回复
热议问题