Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

298
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda