Skip to content

Instantly share code, notes, and snippets.

@timoxley
Created July 27, 2012 17:30
Show Gist options
  • Save timoxley/3189267 to your computer and use it in GitHub Desktop.
Save timoxley/3189267 to your computer and use it in GitHub Desktop.
mongoose drop collections helper
var async = require('async')
var _ = require('underscore')
var Helpers = function(mongoose) {
this.mongoose = mongoose || require('mongoose')
this.dropCollections = function(callback) {
var collections = _.keys(mongoose.connection.collections)
async.forEach(collections, function(collectionName, done) {
var collection = mongoose.connection.collections[collectionName]
collection.drop(function(err) {
if (err && err.message != 'ns not found') done(err)
done(null)
})
}, callback)
}
}
module.exports = Helpers
@SKART1
Copy link

SKART1 commented Mar 19, 2015

It uses native driver - which is not good for consistence. So mongoose will not know that that collections already deleted.

@DScheglov
Copy link

mongoose in closer is assigned incorrectly. Line 5 shoould be:

this.mongoose = mongoose = mongoose || require('mongoose');

or even just like this:

mongoose = mongoose || require('mongoose');

@JuanDelgadillo
Copy link

Thanks! :)

@kelthuzad
Copy link

Wouldn't it be easier to use async.forEachOf to iterate all keys in the object instead of using underscore?

async.forEachOf(mongoose.connection.collections, function(collection, collectionName, done) {
// drop-collection-code
});

That way, you wouldn't need the underscore dependency at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment