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

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

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 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