Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
First, clearly define what it means to have a conflict. If you have a definition, please share it. –  Emmad Kareem Feb 8 '12 at 4:02
    
Conflicts are two objects that have schedules that overlap. –  Sara Chipps Feb 8 '12 at 4:08
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
Sounds like a winner to me. –  Mike Brown Feb 8 '12 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! –  Sara Chipps Feb 8 '12 at 20:59
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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