1

I currently have an SPA built with MERN and I want to improve it further by adding a scheduled update to a particular collection in my MongoDB database by setting a boolean field in all of the documents in a collection to false every midnight.

Can someone point me to the right direction on how to accomplish this?

I want to be able to scale it as well at some point - for example, have a value saved in a document in another collection to indicate the time where these boolean fields will be invalidated in the front end?

I'm using a MERN stack. Thanks for your help!

1

2 Answers 2

2

you can use cron job

const moment = require('moment');
const CronJob = require('cron').CronJob;

const updateCollections = async ()=>{
  await someQueriesServices()
}

new CronJob('0 0 * * *', async () => {
  await updateCollections()
}, null, true, 'America/Los_Angeles');

or you can use setInterval

const timeInSec = moment().endOf('day').valueOf()
const Interval = Date.now() - timeInSec;

setInterval(async ()=>{
    await updateCollections()
},Interval)
1

I usually use node-schedule

const schedule = require('node-schedule');

const j = schedule.scheduleJob('42 * * * *', function(){
  console.log('The answer to life, the universe, and everything!');
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.