简体   繁体   中英

Cleaning database after tests feature

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': 'test@test.pl' })

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.

在此处输入图片说明

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

I managed to sort this out using code which is in the provided screenshot 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM