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

159
Visualizações
How can I delete all objects over 24 hours old using Mongoose?

I want to remove all un-complete accounts in my app where the user signed up, created an account but didn't finish the verification process by uploading certain images...

I am running a cron job that performs this task every 24 hours...

I want to remove all objects where imageUrlOne & imageUrlTwo == "" AND createdAt is older than 24 hours.

My current code isn't working, any idea what I am doing wrong?

THANK YOU

const User = require("../models/user-model")

const removeInactiveAccounts = async () => {
  let twentyFourHours = miliseconds(24, 0, 0)
  //                     hours, min, seconds

  function miliseconds(hrs, min, sec) {
    return (hrs * 60 * 60 + min * 60 + sec) * 1000
  }

  let startDate = Date.now() // todays date
  let endDate = startDate - twentyFourHours // 24 hours ago from today

  try {
 
    await User.deleteMany({
      imageUrlOne: "",
      imageUrlTwo: "",
      createdAt: {
        $gte: startDate,
        $lte: endDate,
      },
    })
  } catch (err) {
    console.log(err.message)
  }
}

module.exports = removeInactiveAccounts

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have your signs a little mixed up.

You're saying greater than "right now", and less than 24 hours ago.

For simplicity, let's say "right now" is 86400. That would make "24 hours ago" 0.

So, you have: createdAt >= 86400 && createdAt <= 0.

Well, that is impossible. No number can be greater than 86,400 and less than 0 at the same time.

You don't need to include the start date at all in this case. Ditch it and just have createdAt less than endDate and you should be good.

about 4 years ago · Juan Pablo Isaza 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