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

158
Views
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 answers
Answer question

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