如何设置使用条件到期?

时间:2019-03-08 12:12:41

标签: node.js mongodb express mongoose

假设我具有以下用户模型:

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
    username: {
        type: String,
        unique: true,
        required: true
    },
    email: {
        type: String,
        required: true,
        match: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
    },
    isVerified: {
        type: Boolean,
        default: false
    },
    password: {
        type: String,
        required: true
    },
    createdDate: {
        type: Date,
        default: Date.now
    }
});

module.exports = mongoose.model('User', schema);

我有一个名为isVerified的字段,它使我可以知道用户是否单击了验证电子邮件。现在,我要实现以下功能:

如果用户未在12小时内验证帐户,则需要从Users集合中自动删除该记录。我看到MongoDB使选项expires可用:

isVerified: {
    type: Boolean,
    default: false,
    expires: 43200 //Remove after 12 hours
}

但是如果MongoDBisVerified,我怎么能告诉false删除记录?

0 个答案:

没有答案