Search code examples
mongodbmeteorschemaavailabilitydatabase

Designing a mongodb/meteor availability schema


I'm trying to build a small site where people can book users for day-long gigs. I am using the accounts package from meteor and my plan is to attach the following:

user.availability: [ 
                     {   
                       status: n, //where n is 0-# of status types (5 atm) : single char rather than float   
                       date: ddmmyyy, //string as well, no need for time as its day long   
                       job_id: {Job} 
                     }, ... 
                   ]

Job just has information like where,what,when.

There are 5 states: neutral, available, explicitly unavailable, booked, hold. Users are naturally neutral and explicitly have to state availability. They choose between the first three states: n,a,u and are only allowed to set availability max two weeks into the future. Job "givers" are the ones that set the other two availability states. A neutral state to me means they don't need a record in the db (user.availability array).

This style sets me up for the following query:

db.users.find({ availability: { status: 'a', date: { $in: search_dates_array }});

And when a user logs in/accesses the site, I can wipe any availability settings they have for dates prior to the current date. Inactive users are pruned on a much longer interval.

I see this solution as beneficial in that it stores minimal amounts of information, requires little maintenance that can be triggered by specific events, and is easy to query. But I'm not 100% sure if this is the correct way to leverage mongodb's strengths. I would appreciate any help on this and if someone could point me to a (relatively) easy to digest article on optimizing mongodb schemas, that'd be awesome.

Thank you.


Solution

  • I would create a separate collection for the availability. If you use MomentJS for the dates, you can have them stored in human readable form, and they are easily searchable/sortable. Extending the user collection is not always the best idea, especially if your sub-elements are arrays of data that will need to be queried and filtered.

    You could use the fullcalendar package for displaying the (day long) events - it will do a lot of the work for you in being able to display month/week/day views as required, and you can colour code the bookings as you like. https://atmospherejs.com/fullcalendar/fullcalendar