How to represent business hours?

655 views
Skip to first unread message

omer

unread,
Apr 7, 2011, 3:46:39 PM4/7/11
to mongodb-user
Hi,

I'm new to mongodb, and still trying to wrap my head around the mongo
way of modeling data.
I need to represent business hours of libraries (for example) in a way
that will allow me to quickly figure out which libraries are open at a
certain time.

My current idea is to have a hash with the day names as keys, and an
array of hashes for the opening hours:
{
mon : [ { start : 7.5, end : 18 } ] ,
tue : [ {start : 7.5, end : 12 }, {start : 16, end : 20}]
...
}
but I'm not sure if and how I can index this representation for
queries such as "who's open now?" to run quickly enough.
Do you think this representation will do the trick?
Can anyone suggest a better representation?

Thanks,
Omer.

George Gritsouk

unread,
Apr 7, 2011, 5:41:23 PM4/7/11
to mongodb-user
I'm also curious to see what people post, but I'm using something like
this:
{
mon: [500, 1700],
tue: [600, 1740],
etc
}

I find that using integers for the hours is a bit neater (I use the
Python driver). I also stopped using "start" and "end" explicitly
since I know my data won't have lunch hours, or anything of the sort.
Just a short array works. That also cleans it up a tad, and
readability doesn't suffer.

--George

Eliot Horowitz

unread,
Apr 8, 2011, 1:16:46 AM4/8/11
to mongod...@googlegroups.com
I think something like

hours : { mon : { open : 10 , close : 20 } , .. }
would work well

then you can query like
db.foo.find( { "hours.mon.open" : { $gt : (current hour) } ,
"hours.mon.close" : { $lt : (current_hour) } } )

this is a problem if something is open from noon till 2 am though
another options is something like
{ hours : { mon : [ false , false , true true ] } }
so you can say
db.foo.find( { "mon.20" : true } )
can make it more granular than an hour if you want

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

omer

unread,
Apr 8, 2011, 10:37:29 AM4/8/11
to mongodb-user
Thanks for the info.
For the sake of understanding mongo better, can anyone explain the
differences (if there are any) between the following representations,
in terms of efficiency in querying:

1. hours : { mon : { open : 10 , close : 20 } , .. }
2. hours : { mon : [ false , false , true true ] }
3. hours : { mon : { { 10 : true }, { 11 : true }, ... }
4. hours : [{ day : mon, start : 10, close : 20 }, ... ]







On Apr 8, 1:16 am, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> I think something like
>
> hours : { mon : { open : 10 , close : 20 } , .. }
> would work well
>
> then you can query like
> db.foo.find( { "hours.mon.open" : { $gt : (current hour) } ,
> "hours.mon.close" : { $lt : (current_hour) } } )
>
> this is a problem if something is open from noon till 2 am though
> another options is something like
> { hours : { mon : [ false , false , true true ] } }
> so you can say
> db.foo.find( { "mon.20" : true } )
> can make it more granular than an hour if you want
>
> On Thu, Apr 7, 2011 at 5:41 PM, George Gritsouk
>

abki

unread,
Apr 8, 2011, 2:25:53 PM4/8/11
to mongodb-user
On 8 avr, 16:37, omer <omer.on.the.r...@gmail.com> wrote:
> Thanks for the info.
> For the sake of understanding mongo better, can anyone explain the
> differences (if there are any) between the following representations,
> in terms of efficiency in querying:
>
> 1. hours : { mon : { open : 10 , close : 20 } , .. }
> 2. hours : { mon : [ false , false , true true ] }
> 3. hours : { mon : { { 10 : true }, { 11 : true }, ... }
> 4. hours : [{ day : mon, start : 10, close : 20 }, ... ]

Héllo,

It surely depends on the request you do, give some example requests.

abki

omer

unread,
Apr 8, 2011, 2:45:26 PM4/8/11
to mongodb-user
Hi abki,

some simple examples would be:
- Find all libraries that are open now (let's say Friday, 2:44pm)
- Find the opening hours for library X

JohnDev

unread,
Apr 14, 2011, 6:31:42 AM4/14/11
to mongodb-user
i'm also interested in this.....any follow up ?

other example
- find closing times of libaries now (friday 17.45)
Reply all
Reply to author
Forward
0 new messages