Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose.js: remove collection or DB

Tags:

mongoose

Is it possible to remove collection or entire db using mongoose.js?

like image 836
WHITECOLOR Avatar asked Jul 12 '12 14:07

WHITECOLOR


2 Answers

Yes, although you do it via the native MongoDB driver and not Mongoose itself. Assuming a required, connected, mongoose variable, the native Db object is accessible via mongoose.connection.db, and that object provides dropCollection and dropDatabase methods.

// Drop the 'foo' collection from the current database mongoose.connection.db.dropCollection('foo', function(err, result) {...});  // Drop the current database mongoose.connection.db.dropDatabase(function(err, result) {...}); 
like image 182
JohnnyHK Avatar answered Nov 11 '22 15:11

JohnnyHK


This can now be done in Mongoose.

MyModel.collection.drop(); 

Hat tip: https://github.com/Automattic/mongoose/issues/4511

like image 31
David L. Walsh Avatar answered Nov 11 '22 13:11

David L. Walsh