Home > database >  Delete documents after some time when match the condition?
Delete documents after some time when match the condition?

Time:11-28

I am building a api with mongoose. I create a user model and I have to add otp functionality. First of all user give me all data with email. Then I store it with label verified:false in user model. Then user give me otp and I have to match the otp that I store in user model. But problem is if the user do not verified then I have to delete that stored data after 5 minutes. How can I do that with mongoose. Anybody can describe it...

Here in word, I am finding for functionality to delete documents or data when verified is false after 5 minutes automatically. How can I add this functionality with mongoose..

This is Stored data

_id:61a1f4c2565e42d0dc16b205
name:"Simon Santana"
firstName:"Simon"
lastName:"Santana"
email:"[email protected]"
phone:"  161199567"
password:"simon198"
avatar:"http://localhost:3001\project1117-1638003906292.JPG"
//Verified is false... Do not delete when it is true..
verified:false
country:"USA"
role:"user"

The full functionality go through automatic way...

Thank you!

CodePudding user response:

You can use partial TTL index directly in mongodb after v.3.2 as follow:

 db.col.createIndex( {createdDate: 1}, {
     expireAfterSeconds: 300, // 5min
     partialFilterExpression: {
         verifiled: false
     }
 });

( Considering you add createdDate field and index it as partial TTL index the document will be deleted after 5min if the verified field is false )

CodePudding user response:

if you are developing the frontend then you should store all these values in a cookie or localstorage and delete it after five minutes and if the user has authenticated then send the request to register a new user. Also be sure to store the other values in state, localstorage or cookies and send a request then

  • Related