Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb opening hours Schema and query for open/closed

How could I store opening hours withing, say, a Venue document so that I could query whether the Venue is currently open.

Each Venue document would contain just an opening and a closing time for each day of the week (maybe just the ones its open?).

I need the ability only to set and list the hours (trivial I guess) and to query based on the current time whether it is open or closed.

Maybe something along these lines? But not sure on the Schema to be honest nor how to query it to check if its open.

hours: {
    mon: {open: 9:30, close: 18:00 },
    tue: {open: 9:30, close: 17:30 },
    ...
}

Any advice would be much appreciated. Thanks.

like image 720
adamK Avatar asked Jul 04 '13 00:07

adamK


People also ask

How to search for records only in a specific hour in MongoDB?

MongoDB Query to search for records only in a specific hour? For this, use the $hour operator. Let us first create a collection with documents with one of the field as date − Following is the query to display all documents from a collection with the help of find () method − Following is the query to search for specific hours.

What does opening hours mean in schema?

openingHours - Schema.org Property Schema.org Property: openingHours - The general opening hours for a business. Opening hours can be specified as a weekly time range, starting with days, then times per day. Multiple days can be listed with commas ',' separating each day.

What is MongoDB time series data?

MongoDB allows you to store and process time series data at scale. Learn how to store and analyze your time series data using a MongoDB cluster. Time series data is generated everywhere from social media to stock tickers to IoT devices.

How do I query in MongoDB shell?

For example, we can query in the MongoDB Shell, mongosh, for a document in the dowJonesTickerData collection using findOne (). MongoDB optimizes the data, as it stores data ordered by time as opposed to the natural order in regular collections. You can improve query performance by adding secondary indexes on the metaField and/or the timeField.


1 Answers

Instead of using actual hours, rather store your time as 0 - 1440, each number being an increment of a minute. Then you'll be able to use $gte and $lte.

So you'd have:

hours: {
  mon: {open: 570, close: 1080 },
  tue: {open: 570, close: 1050 },
  ...
}

That's the strategy that I'm using in my app. Of course there are slightly more efficient ways, like storing all the hours for the week in a single array, but your approach is still good.

like image 146
nevi_me Avatar answered Nov 16 '22 03:11

nevi_me