Cleaning database after tests feature

949 Views Asked by At

I am trying to clean database after each feature however every approach which I tried failed. I tried to remove whole mongo collection, dropDatabase almost everything (I guess)

Including

mongoose.connection.dropDatabase(() => {})

User.remove({ 'local.email': '[email protected]' })

It seems like nothing is happening to the records in database. By the way my database is hosted on mlab.com (its not local database). I am establishing moongose.connection during starting application (node server.js) so there is no need for connecting to db from the hook, I think.

I want to implement this code in provided hook below.

enter image description here

2

There are 2 best solutions below

0
Rachomir On BEST ANSWER

I managed to sort this out using code which is in the provided screenshotenter image description here

2
ByteMaster On

It doesnt matter where are your DB is situated - only difference is host address, port and credentials

So I'm cleaning my collections using deleteMany

const MongoClient = require('mongodb').MongoClient;
...
MongoClient.connect("mongodb://localhost:27017/test_db", function(err, database) {
  if(err) return console.log(util.inspect(err));
  db = database.db("test_db");
  cCol = db.collection("collection_to_be_cleaned");
  if(cCol)
    cCol.deleteMany({}, function(err, result){
      if(err) return console.log(util.inspect(err));
      console.log("cleaned up ", result.deletedCount, " records");
    });    
});