0

I want to reset/delete all entries from all collections of my database, after a certain period of time in MongoDB for NodeJs. What can be the possible methods to do it?

MongoClient.connect(url, { useNewUrlParser: true },  function(err, 
db) {
    if (err) throw err;
    var dbo = db.db("process.env.DB_NAME");
    var myquery = {};
    dbo.collection("TableStatus").deleteMany(myquery, function(err,  
    obj) {
      if (err) throw err;
      console.log(obj.result.n + " document(s) deleted");
    });
});

I have included this code in a function. So maybe there is a way to call this function after a certain period of time?

  • I think the answer you are looking for is something like combination of https://stackoverflow.com/questions/19961387/trying-to-get-a-list-of-collections-from-mongoose and https://stackoverflow.com/questions/28139638/how-can-you-remove-all-documents-from-a-collection-with-mongoose. You can get all collection names, loop through it and remove all documents from each of them by calling `CollectionName.remove({}, callback)` – Ganapati V S Jan 22 '19 at 14:57

1 Answers1

0

TTL indexes were created for this very purpose: https://docs.mongodb.com/manual/core/index-ttl/

Manoj Vadehra
  • 836
  • 4
  • 17