1

So, I've been coding a bot on Discord. But when I tried to make 30 days auto-delete Tags, I been thinking that how do I find a document with a Time Expired Date in miliseconds that higher than current time.

const autoDeleteTag = function () {
 const currentTime = (new Date()).getTime()
 const findTag = tagSchema.findOne({ timeExpired: currentTime })
 if (findTag) return findTag.delete()
}
setInterval(autoDeleteTag(), 100)

But I don't know if this the right code? Because I think sometime it won't delete. Thanks!

KanPots
  • 11
  • 3
  • 1
    Does this answer your question? [MongoDB query for document older than 30 seconds](https://stackoverflow.com/questions/21591125/mongodb-query-for-document-older-than-30-seconds) – thinkgruen Sep 17 '21 at 08:03
  • ` random.create({ random1 }, { expireAfterSeconds: 30 }) ` so I do this, and after 2 minutes, it still doesn't delete – KanPots Sep 17 '21 at 08:45

1 Answers1

0

If you want to compare dates (or values), you can you use the $gt, $lt etc. operators.

For example if you are looking for the older tags than current date:

tagSchema.findOne({ timeExpired: { $lt: currentTime } })
D. Zsolt
  • 97
  • 1
  • 6