How to drop a database with Mongoose?


I'm preparing a database creation script in Node.js and 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 it with Mongoose.

10/15/2017 3:15:39 PM

Accepted Answer

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!

1/20/2014 3:14:07 PM

Mongoose will create a database if one does not already exist on connection, so once you make the connection, you can just query it to see if there is anything in it.

You can drop any database you are connected to:

var mongoose = require('mongoose');
/* Connect to the DB */
    /* Drop the DB */

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow