Skip to content

Instantly share code, notes, and snippets.

@riyadhalnur
Created March 19, 2015 11:03
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 riyadhalnur/64ab146dcb2098e856d4 to your computer and use it in GitHub Desktop.
Save riyadhalnur/64ab146dcb2098e856d4 to your computer and use it in GitHub Desktop.
We have an app running on the MEAN stack and are using MongooseJS as our ORM. Now, we have 2 collections *event* and *eventV2*. We are looking to drop the *event* collection and rename the *eventV2* collection to just *event*.
var mongoose = require('mongoose');
var async = require('async');
var db = 'mongodb://localhost/myawesomeapp';
mongoose.connect(db, function (err) {
if (err) { throw err; }
async.series([_dropCollection, _renameCollection], function (err, result) {
if (err) { throw err; }
console.info('Completed successfully! Result: ' + result);
process.exit();
});
});
function _dropCollection(callback) {
mongoose.connection.db.dropCollection('event', function (err, result) {
if (err) { callback(err); }
callback(null, result);
});
}
function _renameCollection(callback) {
mongoose.connection.db.renameCollection('eventV2', 'event', function (err, result) {
if (err) { callback(err); }
callback(null, result);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment