Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

mongodb move documents to another collection

function insertBatch(collection, documents) {
  var bulkInsert = collection.initializeUnorderedBulkOp();
  var insertedIds = [];
  var id;
  documents.forEach(function(doc) {
    id = doc._id;
    // Insert without raising an error for duplicates
    bulkInsert.find({_id: id}).upsert().replaceOne(doc);
    insertedIds.push(id);
  });
  bulkInsert.execute();
  return insertedIds;
}

function deleteBatch(collection, documents) {
  var bulkRemove = collection.initializeUnorderedBulkOp();
  documents.forEach(function(doc) {
    bulkRemove.find({_id: doc._id}).removeOne();
  });
  bulkRemove.execute();
}

function moveDocuments(sourceCollection, targetCollection, filter, batchSize) {
  print("Moving " + sourceCollection.find(filter).count() + " documents from " + sourceCollection + " to " + targetCollection);
  var count;
  while ((count = sourceCollection.find(filter).count()) > 0) {
    print(count + " documents remaining");
    sourceDocs = sourceCollection.find(filter).limit(batchSize);
    idsOfCopiedDocs = insertBatch(targetCollection, sourceDocs);

    targetDocs = targetCollection.find({_id: {$in: idsOfCopiedDocs}});
    deleteBatch(sourceCollection, targetDocs);
  }
  print("Done!")
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: Start Angular App In Localhost 
Typescript :: array of objects in class c++ 
Typescript :: Scripts may close only the windows that were opened by them 
Typescript :: dart clone list 
Typescript :: typescript array contains string 
Typescript :: get enum value dynamically typescript 
Typescript :: loop trhough list of lists in python and find single elements 
Typescript :: elements without corner css 
Typescript :: Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. 
Typescript :: cubic beziere curve function 
Typescript :: react functional components setstate callback 
Typescript :: ts compile command 
Typescript :: typescript cast string to number 
Typescript :: removing directory and its content bash linux 
Typescript :: type to string typescript 
Typescript :: laravel row exists or null 
Typescript :: s3.bucket objects filter top 10 
Typescript :: path represents file or directory java 
Typescript :: how to create an unknown amount of objects in c++ 
Typescript :: function permutations() kalibrr 
Typescript :: file attachements contac form 7 
Typescript :: typescript event emitter 
Typescript :: does i5 7th generation processor supports windows 11 
Typescript :: rtk configurestore 
Typescript :: numpy select elements from array condition 
Typescript :: what do you need local sccripts for 
Typescript :: create n sublists python 
Typescript :: how to i count objects available in salesforce organization 
Typescript :: how to get remainder in typescript 
Typescript :: accessing python dictionary values with dot 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =