So I have a collections which stores the start and end times for an event and I would like a user to be able to query for events by specifying "filter" start and end times. The query should return any events that intersect with the filter times. I thought I was building the query correctly, but it's not returning the results I want.
Example data (simplified mongo document to get the idea, times are HHMMSS):
{ ..., "start" : "050520", "end" : "051309" } //Should match
{ ..., "start" : "051125", "end" : "051925" } //Should match
{ ..., "start" : "052049", "end" : "052420" } //Should match
{ ..., "start" : "050000", "end" : "050500" }
{ ..., "start" : "052100", "end" : "052721" }
The query I tried was (Forgive me if I missed a bracket here, that's not the issue):
find( { $or : [
{ start : { $gte : "050600", $lt : "052100" } },
{ end : { $lt : "052100", $gt : "050600" } }
] } )
Which only returned one of the three results. Switching the order of the $or parts ( querying against the end times before the start times) gives me two results but not all three. I seem to remember reading that for compound queries mongo throws away results that don't match the first part, before applying the second part of the query, but I thought the $or operator would handle that. Any ideas? Is my query incorrect, is this a limitation of querying in mongo?