Mongodb 我如何安排猫鼬某个地方的营业时间?
我需要为我的数据库设计一种方法,以便能够存储一个地方的不同开放时间,比如酒吧或餐厅。这些开放时间每天都可能不同,他们需要工作时间,比如9:30或8:15,而不仅仅是整数 这就是我想到的:Mongodb 我如何安排猫鼬某个地方的营业时间?,mongodb,mongoose,database-design,schema,Mongodb,Mongoose,Database Design,Schema,我需要为我的数据库设计一种方法,以便能够存储一个地方的不同开放时间,比如酒吧或餐厅。这些开放时间每天都可能不同,他们需要工作时间,比如9:30或8:15,而不仅仅是整数 这就是我想到的: opening_hours: { normal_days: { mon: { start: { type: Number, min: 0, max: 23 }, end: { type: Number, mi
opening_hours: {
normal_days: {
mon: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
tue: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
wed: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
thu: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
fri: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
sat: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
sun: {
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
},
},
special_days: {
date: Date,
name: String,
start: { type: Number, min: 0, max: 23 },
end: { type: Number, min: 0, max: 23 },
is_closed: Boolean,
},
},
不幸的是,我找不到更好的方法。你认为解决这个问题的最佳方案是什么?我认为这个方案没有太多错误(除了不包括23:00-24:00),也就是说: 如果我在设计这个,我不会在属性名称中使用缩写。例如,我使用完整的“星期四”,然后在对象本身中使用类似于
的缩写:“THU”
。我也会避免在任何地方使用0-23小时。大概是
{
...
shorthand: "THU"
startHours: 0-23,
endHours: 0-23,
startMinutes: 0-59,
endMinutes: 0-59,
startInMilliseconds: 0 - 24 * 60 * 60 * 1000 - 1,
endInMilliseconds: 0 - 24 * 60 * 60 * 1000 - 1
}
这将允许您跨时区工作,因为您可以轻松地操纵unix时间。把你的时间分成小时和分钟可以让你达到23:59,加上格式将是${striday.startHours}:${striday.startMinutes}
,而不是从23.75->23:45转换成一些奇怪的数学
当然,这一切都取决于数据的实际用例
opening_hours: {
normal_days: {
monday: {
start: { type: Date, default: new Date().setHours(8, 0, 0, 0) },
end: { type: Date, default: new Date().setHours(4, 30, 0, 0) },
},
tuesday: {
start: { type: Date, },
end: { type: Date, },
},
}
}
查询数据库时,可以分别获取小时或分钟组件。日期对象存储日期、时间和时区。大多数编程语言都有从日期对象解析小时、分钟和时区的函数。MongoDB拥有$hour、$minute和$dayOfWeek等运营商
我曾经遇到过同样的问题,我就是这样实现的:
opening_hours: [{
day: {type Date}, //mon - sun
periods: [{
start: {type Date},
end: {type Date}
}]
}]
periods
是一个数组的原因是
[
{
start: 9h00,
end: 11h00
},
{
start: 13h00,
end: 18h00
}
]
希望这是有意义的…存储unix时代是个好主意。我还考虑将开放时间存储为一个由4位数字组成的字符串(由regex或类似的东西验证),这样我们就可以使用
start:0800,end:1900
,然后使用moment.js对其进行操作。这也行吗?@Anthony timeAsString实际上也不错。我是出于显示的目的考虑这个问题的,但是每次更新开始和结束时间时,您可能都要跳过一些限制。但我想你有一个updateTime()函数,它需要花费数小时和数分钟,为你发挥所有的魔力,所以我想这没什么大不了的。在这种情况下,它肯定会节省一些时间来存储为字符串以用于显示。我不确定我是否喜欢在moment.js中使用它进行实际时间操作的音频,但是我宁愿用数字进行数学运算,用字符串显示。