Implementing Scheduling in Node.js Applications

mongonode.jsscheduling

I'm building an app and the end user will be using an Admin screen to schedule content on the front page. They will be choosing two dates, a start date and an end date.

Obviously I don't want to cycle through the entire collection to see if todays date is between any of the start or end dates every time the front page loads.

Also, don't want the user to be able to schedule two things that overlap each other. There should be a better way than checking for conflicts with each record before saving.

Using Node/Socketio/Express/Mongoose

I could index the collection in Mongo then sort accordingly to test for today's date and make sure there aren't conflicting schedules. Also, I could have a flag that is set once the day changes that signifies current content. I thought there might be a better way.

Does anyone have any thoughts?

Best Answer

Obviously I don't want to cycle through the entire collection

It's not obvious to me. How big is this collection?

Anyway, why would you have to cycle through the entire collection? Put them in a list ordered by expiration time. Since time moves forward, you can discard everything that has expired. So you only have to check if the first element is current. When it expires, discard it and use the next one.

Related Topic