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?

  • 1
    First, clearly define what it means to have a conflict. If you have a definition, please share it.
    – NoChance
    Commented Feb 8, 2012 at 4:02
  • Conflicts are two objects that have schedules that overlap. Commented Feb 8, 2012 at 4:08

2 Answers 2


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.

  • Sounds like a winner to me. Commented Feb 8, 2012 at 20:56
  • It's actually a very small collection :). I just thought there may be a more attractive way I was missing. Great answer, thank you! Commented Feb 8, 2012 at 20:59

It sounds like you can solve this problem with caching. In other words, go ahead and cycle through the whole collection, but only do it when the cache is invalid. Obviously it would depend on the details of your requirements, but something to look at if you can get away with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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