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

296
Views
Checking the last time a request has been made, deleting stuff from database if the time exceeds a minute

I'm trying to make a timer that would check when the last request to a specific path was made and if the last request was made more than a minute ago, I want the script to delete a document from a mongodb database.

I've tried to achieve this with sessions, but I haven't been successful doing so. I've also tried to save the current time to the DB with the request and then checking it, but I don't know how to make the timer "run in the background", if that's even possible. I also want the timer to run per every ID (the ID is included in a table with the request)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Using a TTL index may help you. It would allow you to set an expiry date for documents when they are accessed, and MongoDB will take care of deleting them after the time is up.

Add a TTL index to your database:

db.entites.createIndex({ requestedAt: 1 }, { expireAfterSeconds: 60 });

When a request is made, update the documents you want to expire by setting the current date for the indexed field:

db.entites.updateMany({ requestId }, { $set: { requestedAt: new Date() } });

Any documents with the requestedAt field set, will be deleted 60 seconds after it's set timestamp.

over 4 years ago · Santiago Trujillo Report

0

Here is an option you can try:

For each request, store some details in the variable called last_request_time , for example the timestamp. The last_request_time value is updated for each request.

Run a cron job: The background job runs every 10 seconds (or 15, 20, or 30 seconds). See cron-node .

The job:

 Calculate difference of current_time and last_request_time If difference is greater then 60 seconds: Set the last_request_time value to null Delete document from database
over 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!