I am storing opening times as an array of shifts every day like below:
{
Monday: [{
startTime: {
hour: 8,
minute: 50
},
endTime: {
hour: 20,
minute: 30
}
}];
}
I am trying to retrieve documents between the start and end of a shift, using MongoDB aggregate $match operator like below:
{
$match: {
'Monday.startTime.hour': { // ex: 8
$lte: parseInt(now.format('HH'), 10), // now hours: 18
},
'Monday.startTime.minute': { // ex: 50
$lte: parseInt(now.format('mm'), 10), // now minutes: 40
},
'Monday.endTime.hour': { // ex: 20
$gte: parseInt(now.format('HH'), 10), // now hours: 18
},
'Monday.endTime.minute': { // ex: 30
$gte: parseInt(now.format('mm'), 10), // now minutes: 40
},
}
}
BUT the problem is that we have a shift example shown in the image below, the first match condition:
'Monday.startTime.hour': {
$lte: parseInt(now.format('HH'), 10),
}
will pass as 8 is smaller than 18.
but the second match condition which matches the minutes part:
'Monday.startTime.minute': { // ex: 50
$lte: parseInt(now.format('mm'), 10), // now minutes: 40
},
will fail as 50 is greater than 40
although in real life 08:50 comes before 18:40
