如何使用Mongoose和TTL删除未经验证的用户 [英] How to remove unverified user with Mongoose and TTL
问题描述
我想删除在一段时间内未验证其帐户的用户.用户可以拥有一个Google帐户(通过Google的API)或在本地注册.我只关心当地人.
I would like to remove users that have not verified their accounts during a certain period of time. An user can have a google account (through Google´s API) or register locally. I only care about the locals ones.
我尝试使用TTL进行此操作没有成功,这是我的用户架构.知道有什么问题吗?到目前为止,我还没有删除它们.
I have tried to do this with TTL without success, here is my User schema. Any idea what is wrong? So far I have not managed to remove them.
谢谢.
const userSchema = mongoose.Schema({
local: {
type: {
email: { type: String, unique: true, required: true },
name: { type: String, required: true },
password: { type: String, required: true },
resetPasswordToken: String,
resetPasswordExpires: Date,
verificationToken: String,
verificationExpires: Date,
registrationConfirmed: Boolean
},
required: false
},
google: {
id: String,
name: String,
email: String
},
accountType: String
});
userSchema.index(
{ 'local.verificationExpires': 1 },
{
expireAfterSeconds: 0,
partialFilterExpression: { 'local.registrationConfirmed': false }
}
);
module.exports = mongoose.model('User', userSchema);
推荐答案
首先,我认为您的架构定义不是有效的猫鼬架构. 我删除了local.type.
First, I think your schema definition is not a valid mongoose schema. I removed the local.type.
我还给了verifyExpires日期一个默认日期,有效期为3分钟,您可以更改该值.
Also I gave the verificationExpires date a default date with a expiration of 3 minutes, you can change that value.
所以架构必须是这样的:
So the schema must be like this:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
local: new mongoose.Schema({
email: { type: String, unique: true, required: true },
name: { type: String, required: true },
password: { type: String, required: true },
resetPasswordToken: String,
resetPasswordExpires: Date,
verificationToken: String,
verificationExpires: {
type: Date,
default: () => new Date(+new Date() + 3 * 60 * 1000) //3 minutes
},
registrationConfirmed: {
type: Boolean,
default: false
}
}),
google: {
id: String,
name: String,
email: String
},
accountType: String
});
module.exports = mongoose.model("User", userSchema);
第二,您可以直接在mongodb上创建索引.
Secondly, you can create the index on mongodb directly.
以下是我可以使其正常运行的步骤:
Here are the steps I could make it work:
1-)在用户架构中删除与索引相关的代码.
1-) Remove the index related code in the user schema.
userSchema.index(
{ 'local.verificationExpires': 1 },
{
expireAfterSeconds: 0,
partialFilterExpression: { 'local.registrationConfirmed': false }
}
);
2-)删除用户集合(如果您不想丢失数据,请考虑备份)
2-) Drop users collection (consider backup if you don't want to lose data)
3-)使用诸如MongoDB Compass之类的gui创建用户集合.
3-) Create a users collection using some gui like MongoDB Compass.
4-)在mongodb中创建此索引.
4-) Create this index in mongodb.
db.users.createIndex(
{ 'local.verificationExpires': 1 },
{
expireAfterSeconds: 0,
partialFilterExpression: { 'local.registrationConfirmed': false }
}
)
这将输出如下内容:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
5-)我创建了2个这样的用户:
5-) I created 2 users like this:
{
"_id" : ObjectId("5def4f0499dc104620a3310b"),
"local" : {
"registrationConfirmed" : false,
"_id" : ObjectId("5def4f0499dc104620a3310c"),
"email" : "user2@def.net",
"name" : "user2",
"password" : "123123",
"verificationExpires" : ISODate("2019-12-10T10:56:40.884+03:00")
},
"__v" : 0
}
{
"_id" : ObjectId("5def4eff99dc104620a33109"),
"local" : {
"registrationConfirmed" : false,
"_id" : ObjectId("5def4eff99dc104620a3310a"),
"email" : "user1@def.net",
"name" : "user1",
"password" : "123123",
"verificationExpires" : ISODate("2019-12-10T10:56:35.385+03:00")
},
"__v" : 0
}
6-)我手动将user1的registrationConfirmed设置为true:
6-) I manually set user1's registrationConfirmed to true:
{
"_id" : ObjectId("5def4eff99dc104620a33109"),
"local" : {
"registrationConfirmed" : true,
"_id" : ObjectId("5def4eff99dc104620a3310a"),
"email" : "user1@def.net",
"name" : "user1",
"password" : "123123",
"verificationExpires" : ISODate("2019-12-10T10:56:35.385+03:00")
},
"__v" : 0
}
7-)user2在验证过期通过几秒钟后被删除.
7-) user2 is removed after a few seconds when the verificationExpires passed.
这篇关于如何使用Mongoose和TTL删除未经验证的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!