node.js - mongo TTL data is not deleted when it expires
伊谢尔伦
伊谢尔伦 2017-05-17 09:56:31
0
3
1106

Using mongoose, you can successfully add an expiration time to a document:

However, the data is still not deleted after the set expiration time. The official document says that the mongo background service polls the expiration setting every one minute, but this is no longer a matter of a few minutes delay. It feels like expire has not taken effect. of

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
漂亮男人

I solved this problem myself and re-read the official documentation https://docs.mongodb.com/manu...

The index defined by schema must correspond to the actual data.

             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 } //设置验证码的有效时间为 10 分钟
                    }
                }, {collection: 'sms'}
            );
            let MyModel = db.model('MyModel', myschema);
            let arr = {
                phone: req.body.phone,
                code: code,
                createAt: Date.now()
            }  

The createAt in schema must correspond to the createAt in arr, and the time must be given for it to take effect.

曾经蜡笔没有小新

let mySchema = new mongoose.Schema(...)Try it?

大家讲道理

Some suggestions:

1. First check the actual TTL situation of the index; in the part you posted, I feel that the code above is inconsistent with the screenshot below

2. Look at the ttl part in server.Status

db.serverStatus().metrics.ttl

For reference.

Love MongoDB! Have fun!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template