-
-
Save j-coll/c2b81a7ff8392ec6b5208b5a8bf0d3d3 to your computer and use it in GitHub Desktop.
Base migration script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function migrateCollectionDifferentCollection(inputCollection, outputCollection, query, projection, migrateFunc) { | |
var bulk = db.getCollection(outputCollection).initializeOrderedBulkOp(); | |
var count = 0; | |
var bulkSize = 500; | |
db.getCollection(inputCollection).find(query,projection).forEach(function(doc) { | |
migrateFunc(bulk, doc); | |
if ( bulk.nUpdateOps + bulk.nInsertOps + bulk.nRemoveOps >= bulkSize ) { | |
count += bulk.nUpdateOps + bulk.nInsertOps + bulk.nRemoveOps; | |
print("Execute bulk! " + count); | |
bulk.execute(); | |
bulk = db.getCollection(outputCollection).initializeOrderedBulkOp(); | |
} | |
}); | |
if ( bulk.nUpdateOps + bulk.nInsertOps + bulk.nRemoveOps > 0 ) { | |
count += bulk.nUpdateOps + bulk.nInsertOps + bulk.nRemoveOps; | |
print("Execute bulk! " + count); | |
bulk.execute(); | |
bulk = db.getCollection(outputCollection).initializeOrderedBulkOp(); | |
} | |
if (count == 0) { | |
print("Nothing to do!"); | |
} | |
} | |
function migrateCollection(collection, query, projection, migrateFunc) { | |
migrateCollectionDifferentCollection(collection, collection, query, projection, migrateFunc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment