如何在特定时间后删除集合的记录
我想使用 mongodb 作为数据库为我的 Node js 应用程序创建一个 OTP(一次性密码) 生成 otp 并通过电子邮件发送给客户端后,我想将 otp 作为 hashedOTP 存储在数据库中,并在特定时间(例如 2 分钟)后删除(或过期) 但我遇到了一些问题,我不知道该怎么做 我读过一些关于 TTL 的文章和 mongodb 文档,但我根本不明白它是如何工作的,我猜它只是关于日期类型记录 所以我用 js setTimeout 方法做到了,在小规模下它工作得很好,但因为我没有经验,我不知道它在更大的规模下工作得很好。 这是我制作的模式,我尝试了 hashedOTP 的 expires 和 expiresAfterSeconds 但它不起作用
const adminSchema = new mongoose.Schema({
firstName : {
type : 'string'
},
lastName : {
type : 'string'
},
email : {
type : 'string'
},
password : {
type : 'string'
},
phoneNumber : {
type : 'string'
},
role : {
type : 'string'
},
confirmationToken : {
type : 'string'
},
hasConfirmedEmail : {
type : Boolean,
default : false
},
hashedOTP : {
type : 'string',
}
})
现在第一个问题是在数据库中保存 OTP 是正确的方法吗? 这是我处理 setTimeout 的代码,
try {
const hashedotp = await bcrypt.hash(otp,10)
verifiedAdmin.hashedOTP = hashedotp
await verifiedAdmin.save()
setTimeout(async () => {
console.log('set time out gonna work now ')
verifiedAdmin.hashedOTP = ''
await verifiedAdmin.save()
}, 120000);
我知道没有必要对 otp 进行哈希处理,但我只是这样做了,verifiedAdmi 是我想要为其发送 otp 的用户,它是由上面的模式创建的 我只是觉得这个 setTimeout 不会正常工作,因为它与数据库有很多交互
i want to create an OTP (one time password) for my node js application using mongodb as database
after generating otp and sending that for client with email i want to store the otp as hashedOTP in database and delete (or expire) that after a specific amount of time like 2 minutes
but iam having some problem with that and i dont know how to do it
i have read some articles and mongodb documentations about TTL but i didn't understand that how its working at all and i guess its just about date type records
so i did it with js setTimeout method and in small scales its working fine but because iam not experienced i dont know that it work fine with biger scales or not
here is my schema that i made and i tried expires and expiresAfterSeconds for hashedOTP but it didnt worked
const adminSchema = new mongoose.Schema({
firstName : {
type : 'string'
},
lastName : {
type : 'string'
},
email : {
type : 'string'
},
password : {
type : 'string'
},
phoneNumber : {
type : 'string'
},
role : {
type : 'string'
},
confirmationToken : {
type : 'string'
},
hasConfirmedEmail : {
type : Boolean,
default : false
},
hashedOTP : {
type : 'string',
}
})
now the first question is saving OTP in database is correct way?
here is my code for handling with setTimeout
try {
const hashedotp = await bcrypt.hash(otp,10)
verifiedAdmin.hashedOTP = hashedotp
await verifiedAdmin.save()
setTimeout(async () => {
console.log('set time out gonna work now ')
verifiedAdmin.hashedOTP = ''
await verifiedAdmin.save()
}, 120000);
i know that it is not necessary to hash the otp but i just did it and the verifiedAdmi is the user that i want to send otp for it and its made by above schema
i just feel that this setTimeout is not going to work fine cause it has many interactions with database
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要在固定时间(即120000秒)之后删除HashEDOTP,则可以运行CRON作业并通过检查创建时间
1-运行CRON作业
2来更新集合,如果(如果检查时间大于create + createat + 120000 SEC)然后删除
hashotp
我更喜欢使用Moment.js检查时间差的
If you want to delete the hashedOTP after a fixedTime (ie 120000 second) , You could run a cron job and update the collection by checking against the createdAt time
1 - Run Cron Job
2 - if( time of checking is greater than createdAt + 120000 sec) then delete
the hashotp
i prefer using moment.js to check time difference
您没有删除您执行以下操作:
创建一个不同的模型,请说,
otpverification
带有字段userId
(索引,因为将完成很多搜索), otp (随机数)和expiresat
(当前时间 + 10分钟可以使用Moment JS轻松完成)。然后,在验证
otpverification
中查找的同时,如果有userId
提供的任何文档,则如果现在检查otp
和Instead of deleting you do do the below:
Create a different model let say ,
OtpVerification
with fieldsuserId
(indexed, because lot of searching will be done), otp (the random number) andexpiresAt
(current time + 10 minutes can be done easily using moment js).Then while verifying find in
OTPVerification
if any document with thatuserId
present if present checkotp
and