I am having multiple collections inside a single database. I have basically created collections name by current date like 20220829, 20220830, 20220831 (these are collection names). I want to keep only 20220831 collection (the latest one) and basically delete all others in that database. Is there a way to do this optimally?
CodePudding user response:
Please refer following code snip
afterEach(async function () {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
let todaysDate = yyyy mm dd
const collections = await mongoose.connection.db.collections()
for (let collection of collections) {
if(collection !== todaysDate)
await collection.remove();
}
})