I want to store Documents with HourTime parameter. The requirement is to be store HourTime of opening hour of supermarket and I need the option to query "Which supermarket is open now"?
what the best practice to do that and how to develop it? I use nodejs, express and mongoose. thanks
CodePudding user response:
You can use hour and minute. First you need to create the fields, if you have multiple times depeding on week day you can also use an array.
{
_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
}
With a model like this you can do a find:
{
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
}
The params for the query in find you can achieve using time with day.js, for example dayjs().format("HH") will retrive the actual hour where your node is running.