如何用mongoose模式存储HourTime参数?

6gpjuf90  于 2023-01-21  发布在  Go
关注(0)|答案(1)|浏览(118)

我想用HourTime参数存储文档。要求是存储超市营业时间的HourTime,我需要查询“哪家超市现在营业”的选项?
做这件事的最佳实践是什么?如何开发它?我使用nodejs,express和mongoose。谢谢

llycmphe

llycmphe1#

你可以使用小时和分钟。首先你需要创建字段,如果你有多个时间依赖于工作日,你也可以使用数组。

{
    _id: ObjectId("600631c7c8eb4369cf6ad9c8"),
    name: "Market X",
    openHour: 10, // number between 0 and 23
    openMinute: 30 // number between 0 and 59,
    closeHour: 18, // number between 0 and 23,
    closeMinute: 30, // number between 0 and 59
    weekDay: 1 // number beetween 1 and 7 
}

使用这样的模型,您可以进行查找:

{
  weekDay: 1, // number between 1 (Monday) and 7 (Sunday)
  openHour: { $lte: 11 }, // number between 0 and 23
  openMinute: { $lte: 32 }, // number between 0 and 59
  closeHour: { $gte: 17 }, // number between 0 and 23
  closeMinute: { $gte: 20 } // number between 0 and 59
}

find中的查询参数可以通过time和day.js实现,例如dayjs().format(“HH”)将检索节点运行的实际时间。

相关问题