I am looking for a good pattern to handle cron-type data in a MongoDB collection (or collections). I have been peeking at the O’Reilly book MongoDB Applied Design Patterns and poking around the MongoDB site’s Use Cases site but neither seems to to cover this type of data.
Is there a good pattern for storing repeating schedules that would lend itself to being able to find documents matching the current schedule, or should I simply store the data same as crontab and do all the calculations in-app?
I think you’re going to be better off just storing the data as is; let MongoDB work its magic on top of that. Mongo’s internal structures ought to handle identifying the repeating patterns for you, based upon the key-value-pair structures you choose to represent the data.
Trying to create your own means of identifying repeating patterns duplicates the work that the non-relational DB can provide, and is also likely to start wrapping itself around the axle for edge cases. You’ll also have to provide that translation functionality for every job entry you wish to track within MongoDB.
To be more specific, I’m suggesting setting up a key value pair for each of the traditional fields used in specifying the cron job. So a given job entry in your MongoDB schema would have values for the frequency, minute, hour, day, etc…
Your queries would then be able to check for “where frequency equals …” and “where day equals …” and so on.
If you look at this mongodb tutorial on querying timestamps, you’ll find that it’s pretty trivial to specify ranges in order to find recurring patterns within the data. And while that tutorial is focused on finding timestamps within blog posts, it would be pretty easy to alter the key-value pairs they used to reflect the data you have from the cron jobs.
If you’re stuck on the particular format of the data, then you may also want to look at this StackOverflow question as it is fairly similar to yours. The accepted answer highlights the standard cron job specification syntax and provides an ACM link to the cron data structure. Other answers suggest other formats such as iCalendar.
0