I have a problem when I want to query and compare nested documents and their fields within that document. Overall I have a database with restaurants. Each of the restaurants have a document called "openingHours"
that contains a document for each day of the week with 3 fields
1 The day of the week
2 Opening time
3 Closing time
In my query the goal is to compare the opening time
and closing time
, for each of the documents (days).
The problem right now is that when I run my query I get a result based on all the documents inside "openingHours"
and it doesn't compare each of the documents on its own. Reason behind the "$expr"
is that the time the restaurants close can extend to 01:00 so we need to check for that as well.
Here is my query:
$or: [
{
$and: [
{
"$expr" : {
"$gt" : [
"$openingHours.open",
"$openingHours.close"
]
}
},
{
"openingHours.close": {$gte: openingHours}
}
]
},
{
$and: [
{
"$expr" : {
"$gt" : [
"$openingHours.open",
"$openingHours.close"
]
}
},
{
"openingHours.close": {$lte: openingHours}
},
{
"openingHours.open": {$lte: openingHours}
}
]
},
{
$and: [
{
"$expr" : {
"$lt" : [
"$openingHours.open",
"$openingHours.close"
]
}
},
{
"openingHours.open": {$lte: openingHours}
},
{
"openingHours.close": {$gte: openingHours}
}
]
},
]
})
Screenshot from Robo3t to help show what I mean with the documents. Also you can see in the image that the problem occurs when it doesn't make the query on each of the nested documents itself, since the time can differ from day to day.
Thanks in advance!