Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

360
Views
How to delete this field after 5m automatically on mongoDB ? mongodb, mongoose ,schema

this is my schema i want to automatically expire this field after 5 minutes. how i do that?

const userRegistrationSchema = new mongoose.Schema({

    mobile: {
        type: Number, trim: true,
    },
    email: {
        type: String, lowercase: true, trim: true
    },
    // email Varification
    verified: {
        type: Boolean,
    },
    otp: {
        type: Number,
    },
    password: {
        type: String, trim: true,
    },
    confirmPassword: {
        type: String, trim: true,
    },

if user enter this value and otp store like this

mobile: 9561300851
email: "xyz@gmail.com"
verified: false
otp: 8236

and after 5 minutes i want to my data look like with otp field

mobile: 9561300851
email: "skcoder510@gmail.com"
verified: false

I hope I have explained my problem then how to do it.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

MongoDB supports documents deletion only after a certain time using TTL. However for field deletion what you can do is after using the OTP you can remove the field using $unset.

https://www.mongodb.com/docs/manual/reference/operator/update/unset/

Or, you can create scheduler which will run after certain time and remove all the used OTP field using $unset.

about 4 years ago · Santiago Trujillo Report

0

AFAIK MongoDB only supports TTL on documents, not on fields: https://www.mongodb.com/docs/manual/core/index-ttl/

You could consider creating a separate OTP schema that has a TTL index of 5 minutes. The relevant Mongoose documentation can be found here: https://mongoosejs.com/docs/api.html#schemadateoptions_SchemaDateOptions-expires

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!