Thanks for the VERY detailed answer
I found 9 use cases for this query, I test the other 4 and here is the total result:
5 use cases - Passed OK.
4 use cases - Failed
Here are the details for the last 3 use cases:
4 test:
2014-06-29 17:31:00.000 - 2014-06-29 18:32:00.000 - overlap with 2nd and 3rd items. The start date&time is where NO event occur but the end date&time is in existing event
{
"$or": [
{"$and": [ {"EventStartDate": {"$gt": "2014-06-29 17:31:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 18:32:00.000"} } ] },
{"$and": [ {"EventStartDate": {"$lt": "2014-06-29 17:31:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 18:32:00.000"} } ] }
]
}
Return 3 items (means no event exists at this time) - Failed.
5 test:
2014-06-29 13:31:00.000 - 2014-06-29 15:32:00.000 - overlap with 1st items. The start date&time is where event occur but the end date&time is where NO event occur
{
"$or": [
{"$and": [ {"EventStartDate": {"$gt": "2014-06-29 13:31:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 15:32:00.000"} } ] },
{"$and": [ {"EventStartDate": {"$lt": "2014-06-29 13:31:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 15:32:00.000"} } ] }
]
}
Return 3 items (means no event exists at this time) - Failed.
6 test:
2014-06-29 13:31:00.000 - 2014-06-29 18:32:00.000 - overlap with 1st, 2nd and 3rd items. The start date&time is where 1st event occur and the end date&time is where 2nd and 3rd events occur
{
"$or": [
{"$and": [ {"EventStartDate": {"$gt": "2014-06-29 13:31:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 22:32:00.000"} } ] },
{"$and": [ {"EventStartDate": {"$lt": "2014-06-29 13:31:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 22:32:00.000"} } ] }
]
}
Return 1 items while its overlap all 3 events - Failed.
7 test:
2014-06-29 13:31:00.000 - 2014-06-29 22:32:00.000 - overlap with 1st, 2nd and 3rd items. The start date&time is where 1st event occur and the end date&time is AFTER 2nd and 3rd events occur
{
"$or": [
{"$and": [ {"EventStartDate": {"$gt": "2014-06-29 13:31:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 18:32:00.000"} } ] },
{"$and": [ {"EventStartDate": {"$lt": "2014-06-29 13:31:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 18:32:00.000"} } ] } ] }
Return 3 items (means no event exists at this time) - Failed.
8 test:
2014-06-29 09:31:00.000 - 2014-06-29 22:32:00.000 - overlap with 1st, 2nd and 3rd items. The start date&time is BEFORE 1st event occur and the end date&time is AFTER 2nd and 3rd events occur (means - all DB entries)
{
"$or": [
{"$and": [ {"EventStartDate": {"$gt": "2014-06-29 09:31:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 18:32:00.000"} } ] },
{"$and": [ {"EventStartDate": {"$lt": "2014-06-29 09:31:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 18:32:00.000"} } ] } ] }
Return 0 items (means no event exists at this time) - Passed OK.
9 test:
2014-06-29 09:31:00.000 - 2014-06-29 15:32:00.000 - overlap with 1st item (the start date&time occur before event and the end date&time occur after the event - surrounding the event)
{
"$or": [
{"$and": [ {"EventStartDate": {"$gt": "2014-06-29 09:31:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 15:32:00.000"} } ] },
{"$and": [ {"EventStartDate": {"$lt": "2014-06-29 09:31:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 15:32:00.000"} } ] }
]
}
Return 2 items - Passed OK.
So in total: 5 Passed - OK, 4 Failed,
Can you please check?
In addition I notice that in case I will replace all places that I have "and" with "or" and all place with "or" to "and" it will return the event/s that overlaped or blank in case this time is available, is it correct?
i.e.
{
"$and": [
{"$or": [ {"EventStartDate": {"$gt": "2014-06-29 14:21:00.000"} }, {"EventEndDate": {"$gt": "2014-06-29 14:22:00.000"} } ] },
{"$or": [ {"EventStartDate": {"$lt": "2014-06-29 14:21:00.000"} }, {"EventEndDate": {"$lt": "2014-06-29 14:22:00.000"} } ] }
]
}
p.s. while doing it we still face the same error as for test 4 and 6 (means no items return back)