Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda