What is the best practice to store weekdays in MongoDB?

779 views Asked by At

I'm looking for the best practice, to store weekdays in a document.

In relational database, 1-n n-n relationships mean 2 or more tables. But in non-relational database, since it is possible to directly store those things into one model, I want to query all possible schedules for Tuesday:

// schedule document:
{
    "_id" : ObjectId("..."),
    "name" : "Have a drink with Joe Schmoe at 9pm"
    "weekdays" : ['MON', 'TUE', 'SAT', 'SUN'] // or
    "weekdays" : ['1', '2', '6', '7'] // ?
    "created" : ISODate("2013-12-03T12:45:53.000Z")
}
1

There are 1 answers

0
str On BEST ANSWER

I would store them as numbers (not number strings as you proposed). That way you could easily sort different schedules according to the week day.

Also note that in some countries, the calendar week starts with Sunday and some date functions thus require Sunday to be number 0 (or 1, depending on the programming language). But it is also very easy to just change the number scheme before using any date functions, anyway.