node.js - mongo TTL 數(shù)據(jù)過期不刪除
問題描述
使用 mongoose 可以成功為某個(gè) document 添加過期時(shí)間:

但是,數(shù)據(jù)在設(shè)置的過期時(shí)間之后仍然沒有刪除,官方的文檔說mongo后臺服務(wù)每隔一分鐘輪詢一次過期設(shè)置,但這已經(jīng)不是幾分鐘延遲的事了,感覺上是expire沒有生效的
問題解答
回答1:這個(gè)問題我自己解決了,重新看了下官方文檔 https://docs.mongodb.com/manu...
schema定義的索引必須與實(shí)際的數(shù)據(jù)相對應(yīng)。
let myschema = new mongoose.Schema({ phone: {type: String,required: true }, code: {type: String,required: true }, createAt: {type: Date,default: Date.now(),index: { expires: 60*1 } //設(shè)置驗(yàn)證碼的有效時(shí)間為 10 分鐘 }}, {collection: ’sms’} ); let MyModel = db.model(’MyModel’, myschema); let arr = {phone: req.body.phone,code: code,createAt: Date.now() }
schema 中的 createAt 必須與 arr 中的 createAt 對應(yīng),且必須給定時(shí)間,這樣才會生效。
回答2:let mySchema = new mongoose.Schema(...)試試?
回答3:幾點(diǎn)建議:
1、先檢查一下索引的實(shí)際TTL情況;您貼出來的部分,感覺上面代碼和下面的截圖不一致
2、看看server.Status中的ttl的部分
db.serverStatus().metrics.ttl
供參考。
Love MongoDB!Have fun!
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問4. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?5. 實(shí)現(xiàn)bing搜索工具urlAPI提交6. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?7. css - 寫頁面遇到個(gè)布局問題,求大佬們幫解答,在線等,急!~8. phpstudy8.1支持win11系統(tǒng)嗎?9. 配置Apache時(shí),添加對PHP的支持時(shí)語法錯(cuò)誤10. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化

網(wǎng)公網(wǎng)安備