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

245
Views
How can I make a mongoDB schema delete in 5 min

So im making a verification code sorta thing, they get the code then they can verify. But, I want to make it so it expires in 5 min (so the db isnt filled with random stuff) after creation how would I go about doing that.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can add expireAt as time-to-live parameter with createIndex.

To create a TTL index, use the createIndex() method on a field whose value is either a date or an array that contains date values, and specify the expireAfterSeconds option with the desired TTL value in seconds.

db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 } )

Document will expire in 1 hour and will be deleted automatically by mongodb.

Check this documentation | TTL indexes

Update:

In mongoose you can this parameter directly in Schema constructor

const schema = Schema({ name: String, timestamp: Date, metadata: Object }, {
  expireAfterSeconds: 86400
});
about 4 years ago · Juan Pablo Isaza 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!