search

Home  >  Q&A  >  body text

node.js - mongo TTL data is not deleted when it expires

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

伊谢尔伦伊谢尔伦2817 days ago1097

reply all(3)I'll reply

  • 漂亮男人

    漂亮男人2017-05-17 09:58:31

    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.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-17 09:58:31

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-17 09:58:31

    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!

    reply
    0
  • Cancelreply