Mongoose: set expire but data don't delete

Created on 16 May 2017  ·  1Comment  ·  Source: Automattic/mongoose

I set expire for one document like this:

            let db = mongoose.createConnection(mongoConfig.mongodb);
            db.on('error', console.error.bind(console, 'connection error: '));
            db.once('open', () => { console.log('mongodb connected');});
            let myschema = new mongoose.Schema(
                {
                    phone: {
                        type: String,
                        required: true
                    },
                    code: {
                        type: String,
                        required: true
                    },
                    g2: {
                        type: Date,
                        default: Date.now,
                        index: {expires: 60*1} 
                    }
                }, {collection: 'sms'}
            );

            let MyModel = db.model('MyModel', myschema);
            let arr = {
                phone: req.body.phone,
                code: code,
            }

            MyModel.findOneAndUpdate({phone: req.body.phone}, arr, {upsert: true}, (err, result) => {
                console.log('db result: ', result);
                if (err) {
                    console.log('database error: ', err);
                } 
            });

and i getIndexes through mongo shell :

> db.sms.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "caibao.sms"
        },
       {
                "v" : 2,
                "key" : {
                        "g2" : 1
                },
                "name" : "g2_1",
                "ns" : "caibao.sms",
                "expireAfterSeconds" : 60,
                "background" : true
        }

i think it shoud work, but actually this document don't delete after 60s and even 30min. I don't kown what's wrong with my code.

Most helpful comment

I solved it.

             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()
            }  

createAt must match in schema and my arr , and be given a time.

>All comments

I solved it.

             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()
            }  

createAt must match in schema and my arr , and be given a time.

Was this page helpful?
0 / 5 - 0 ratings