我在一個資料庫中有多個集合。我基本上已經按當前日期創建了集合名稱,例如 20220829、20220830、20220831(這些是集合名稱)。我只想保留 20220831 集合(最新的一個)并基本上洗掉該資料庫中的所有其他集合。有沒有辦法以最佳方式做到這一點?
uj5u.com熱心網友回復:
請參考以下代碼片段
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();
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504010.html
