Yaron Naveh Yaron Naveh - 1 year ago 202
Node.js Question

drop database with mongoose

I'm preparing a db creation script in node.js + mongoose.
How can I check if the database already exists, and if so drop (delete) it using mongoose?
I Could not find a way to drop with mongoose.

Answer Source

There is no method for dropping a collection from mongoose, the best you can do is remove the content of one :

Model.remove({}, function(err) { 
   console.log('collection removed') 

But there is a way to access the mongodb native javascript driver, which can be used for this

mongoose.connection.collections['collectionName'].drop( function(err) {
    console.log('collection dropped');


Make a backup before trying this in case anything goes wrong!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download