Shaheer
Shaheer

Reputation: 49

MongoDb Database design for storing week days and times

I am new to MongoDb. I am working on health website where I am storing doctors data. I have problem in storing hospitals data for doctor. A doctor is available in a hospital for 3 or 5 days in a week for a specific time. for example A doctor is available in Hospital X on monday,tuesday and friday, from 10AM to 3PM. My current schema is given below which is storing only hospital name,address, startTime,endTime, But Here I am not specifying the week days.

hospitals:[{
    hospitalName:{type:String, required:true},
    hospitalAddress:{type:String, required:true},
    startTime:{type:String, required:true},
    endTime:{type:String, required:true},
    city:{type:String,required:true},
    fee:{type:String, required:true}
}]

Now Please guide me that what is the best approach to store week days and timmings in mongoDb. Is that good approach if I have output like that

{hospitalName:'XYZ',
hospitalAddress:'XYZ',
days:{
mon:{startTime:'10:00am' , endTime:'1:00pm', slots:['10:00','10:10','10:20']}
tue:{startTime:'10:00am' , endTime:'1:00pm', slots:['10:00','10:10','10:20']}
friday:{startTime:'10:00am' , endTime:'1:00pm', slots:['10:00','10:10','10:20']}
}
fee:'2000'
} 

Please Share best schema design approaches for the above output.

Upvotes: 1

Views: 701

Answers (1)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59456

Don't use any local stings for days or times!

When you follow the ISO-8601 standard, it will make your life easier. I would do it like this:

days: [
   { weekday: 1, startHour: 10, startMinute: 0, endHour: 13, endMinute: 00, slots: [{h:10, m:0}, {h:10, m:10}, {h:10, m:20} ] },
   { weekday: 2, startHour: 10, startMinute: 0, endHour: 13, endMinute: 00, slots: [{h:10, m:0}, {h:10, m:10}, {h:10, m:20} ] },
   { weekday: 5, startHour: 10, startMinute: 0, endHour: 13, endMinute: 00, slots: [{h:10, m:0}, {h:10, m:10}, {h:10, m:20} ] }
]

Or course, you could also use this:

days: [
   { weekday: 1, startTime: "10:00", endTime: "13:00", slots: [ '10:00', '10:10', '10:20' ] },
   { weekday: 2, startTime: "10:00", endTime: "13:00", slots: [ '10:00', '10:10', '10:20' ] },
   { weekday: 5, startTime: "10:00", endTime: "13:00", slots: [ '10:00', '10:10', '10:20' ] }
]

However, assume you need to create real Date values from it. It would be

{
    $dateFromParts : {
        isoWeekYear: { $isoWeekYear: "$$NOW" }, 
        isoWeek: { $isoWeek: "$$NOW" }, 
        isoDayOfWeek: "$weekday",
        hour: "$startHour", 
        minute: "$startMinute", 
        timezone: ...
    }
}

compared to

{
    $dateFromParts : {
        isoWeekYear: { $isoWeekYear: "$$NOW" }, 
        isoWeek: { $isoWeek: "$$NOW" }, 
        isoDayOfWeek: "$weekday",
        hour: { $toInt: { $arrayElemAt: [ { $split: [ "$startTime", ":" ] }, 0 ] } }, 
        minute: { $toInt: { $arrayElemAt: [ { $split: [ "$startTime", ":" ] }, 1 ] } }, 
        timezone: ...
    }
}

If you use local day names, then you need to write your own weekday lookup table. MongoDB does not natively support any weekday names.

Upvotes: 2

Related Questions