Sunday, 1 April 2018

Resetting one field in mongo collection to 0 every 24 hours

In my app the user has a limit of the amount of profiles they can heart every day. I'm trying to set the field "heartedNum" back to 0 every 24 hours. I ran this code to test and it works.setInterval(async function() { await Users.updateMany( {}, { $set: { heartedNum: 0 } }, { upsert: true } ); }, 10000); Every 10 seconds heartedNum gets set back to 0 in my mongo database. But I was wandering how secure this is. If I scaled the 10 seconds up to 24 hours. I tried to use a cron as well:var CronJob = require("cron").CronJob; But it didn't recognise the Users schema? Is it best to perform this with Cron and if so how do I get it to work with Users collection. Thanks a lot :)

Submitted April 01, 2018 at 10:47AM by harrydry

No comments:

Post a Comment