Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 932
Yaron Naveh Avatar asked Apr 10 '12 00:04

Yaron Naveh


People also ask

How do I drop a database in MongoDB?

The best way to do it is from the mongodb console: > use mydb; > db. dropDatabase(); Alternatively, you can stop mongod and delete the data files from your data directory, then restart.

Is it possible to drop database in node JS?

js – Drop Database in MongoDB. We can delete/drop a database from MongoDB through Node. js program.

What does save () do in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.


1 Answers

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'); }); 

Warning

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

like image 180
drinchev Avatar answered Nov 15 '22 23:11

drinchev