Mongoose.js: remove collection or DB

Mongoose

Mongoose Problem Overview


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

Mongoose Solutions


Solution 1 - Mongoose

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) {...});

Solution 2 - Mongoose

This can now be done in Mongoose.

MyModel.collection.drop();

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

Solution 3 - Mongoose

For those that are using the mochajs test framework and want to clean all db collections after each test, you can use the following which uses async/await:

afterEach(async function () {
  const collections = await mongoose.connection.db.collections()

  for (let collection of collections) {
    await collection.remove()
  }
})

Solution 4 - Mongoose

Mongoose references the connection on every model. So, you may find it useful to also drop the DB or collection off of an individual model.

For example:

// Drop the 'foo' collection from the current database
User.db.dropCollection('foo', function(err, result) {...});

// Drop the current database
User.db.dropDatabase(function(err, result) {...});

Solution 5 - Mongoose

For 5.2.15 version of Mongoose + Mocha tests usage where you need to drop all collections before each test.

beforeEach(async () => {
     const collections = await mongoose.connection.db.collections();

     for (let collection of collections) {
          // note: collection.remove() has been depreceated.        
          await collection.deleteOne(); 
     }
});

Solution 6 - Mongoose

If want to drop collection after tests and your test were running in a docker container:

mongoose = require("mongoose");
...
afterAll(async () => {
  const url = 'mongodb://host.docker.internal:27017/my-base-name';
  await mongoose.connect(url)
  await mongoose.connection.collection('collection-name').drop()
})

Solution 7 - Mongoose

I dropped my collections using Connection.prototype.dropCollection()

const conn = mongoose.createConnection('mongodb://localhost:27017/mydb');
conn.dropCollection("Collection_name",callbacks);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWHITECOLORView Question on Stackoverflow
Solution 1 - MongooseJohnnyHKView Answer on Stackoverflow
Solution 2 - MongooseDavid L. WalshView Answer on Stackoverflow
Solution 3 - MongooseadamcView Answer on Stackoverflow
Solution 4 - MongooseHuston HedingerView Answer on Stackoverflow
Solution 5 - MongooseDurjaView Answer on Stackoverflow
Solution 6 - MongooseVitalii AndrusishynView Answer on Stackoverflow
Solution 7 - MongooseMitrajit ChandraView Answer on Stackoverflow