Skip to content

Instantly share code, notes, and snippets.

@j-coll
Forked from pfurio/migrateCollection.js
Last active June 16, 2017 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-coll/c2b81a7ff8392ec6b5208b5a8bf0d3d3 to your computer and use it in GitHub Desktop.
Save j-coll/c2b81a7ff8392ec6b5208b5a8bf0d3d3 to your computer and use it in GitHub Desktop.
Base migration script
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