0

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.

enter image description here

Rachomir
  • 280
  • 1
  • 5
  • 16

2 Answers2

0

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");
    });    
});
ByteMaster
  • 172
  • 6
  • you tested or you just afraid? It's working peace of code. Clarify what you already have tried to do with piece of code not just lines – ByteMaster Jan 26 '18 at 23:46
  • I tried and Its saying database.db("name_of_db") is not a fucntion. I am happy to let you in to my priv Github repo so you can sort it out If you know how to do that – Rachomir Jan 27 '18 at 13:27
0

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

Rachomir
  • 280
  • 1
  • 5
  • 16