Home > Back-end >  Mongoose findOne() with a value higher than a var
Mongoose findOne() with a value higher than a var

Time:09-17

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!

CodePudding user response:

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 } })
  • Related