假设我具有以下用户模型:
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
}
但是如果MongoDB
是isVerified
,我怎么能告诉false
删除记录?