Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recurring Events Schema w/ MongoDB

I've read some of the papers out there describing data storing methods for recurring events, but I'm still trying to wrap my head around the best practice, especially concerning MongoDB.

My main concern is to cheaply retrieve all of the events that occur within a given timeframe. My secondary concern is to modify and alter single occurrences without taking the entire event chain out of whack.

Looking at others who have asked similar questions, I have come up with a possibility. I'm not completely sold on it, and would love some pointers in the right direction.

My Idea: Within each Event document, have...

  • a recurring string field that closely matches the iCal standard
  • an "occurrences" embedded document or array field that contains changes/edit on specific occurrences (such as changing the description or start time, or canceling a single occurence).
  • an occurrence start and end field to define easily queried boundaries of the recurrence rule

Pros:

  • able to store changes and still maintain association to other events
  • easily queried, my model on the business side would have to construct each event though

Cons/Potential Problems:

  • if editing an event, and a user decides to mark changes as applying to "all events" - how to keep events that have already past from being altered
like image 363
Conrad Vanlandingham Avatar asked Jun 05 '12 01:06

Conrad Vanlandingham


People also ask

How are recurring events stored in a database?

Add a recurrence domain to the database that supports a number of different values, including “daily”, “weekly”, and “monthly”. Add a recurrence column to the events table that identify how an event recurs. Add a recurrence_dates table that contains a pre-generated list of recurrences for a given date.

What is a recurring event?

A recurring event is an event that happens more than once, on a repeating schedule. When a repeating event is turned into individual event instances with individual dates, it is called “expanding” the event.


1 Answers

This seems like a good approach to me. To "keep events that have already past from being altered" simply mark them with a boolean flag that says so. You should easily be able to use that flag and the start/end date while querying and updating.

Alternatively, you could: - set an end date for the original event - clone the event, and set a new start and end date on the new event. - empty out the occurences field on the cloned event

Something like doing this:

Before:

{
    'title' : "Gin O'Clock",
    'recurrance' : 'DAILY',
    'start_date' : '2012-01-01 17:00',
    'end_date' : false,
    'occurences' : [
        { 'date' : '2012-06-03 17:00', 'title' : "Jubilee Gin O'Clock" }
    ]
}

After:

{
    'title' : "Gin O'Clock",
    'recurrance' : 'DAILY',
    'start_date' : '2012-01-01 17:00',
    'end_date' : '2012-06-05 17:00,
    'occurences' : [
        { 'date' : '2012-06-03 17:00', 'title' : "Jubilee Gin O'Clock" }
    ]
},
{
    'title' : "Gin O'Clock an our earlier",
    'recurrance' : 'DAILY',
    'start_date' : '2012-06-06 16:00',
    'end_date' : false,
    'occurences' : [
    ]
}

Hope that helps!

like image 56
Derick Avatar answered Sep 29 '22 16:09

Derick