I am trying to search time between two time ranges disregarding the date (year,day,month) do not matter only time
I was able to reach to this point by myself but was not able to find any solution to consider only time unless I change my schema
StoreModel.aggregate([
{ $unwind: { path: "$practitioners" } },
{
$match: {
_id: new Types.ObjectId(storeId),
"practitioners._id": new Types.ObjectId(practitionerId),
},
},
{
$project: {
_id: false,
timings: "$practitioners.schedule.timings",
},
},
{
$set: {
dateInTimings: {
$let: {
vars: {
my_date: date,
},
in: {
$reduce: {
input: "$timings",
initialValue: false,
in: {
$cond: [
{ $not: ["$$value"] },
{
$and: [
{
$gte: ["$$my_date", "$$this.from"],
},
{
$lte: ["$$my_date", "$$this.to"],
},
],
},
"$$value",
],
},
},
},
},
},
},
},
]);
CodePudding user response:
Query
- use
$hour,$minute,$seconds
ignore theyear/month/day
- compare
[hours1,minutes1,milliseconds1] > [hours2,minutes2,milliseconds2]
it will compare the members from left to right for the 2 dates
*i think this works as a way to compare only the times of 2 dates, but maybe a faster or simpler way exists
aggregate(
[{"$set":{"date1":ISODate("2022-05-17T22:50:44.501Z")}},
{"$set":{"date2":ISODate("2022-05-17T22:49:44.501Z")}},
{"$set":
{"date1Bigger":
{"$gt":
[[{"$hour":"$date1"}, {"$minute":"$date1"},
{"$millisecond":"$date1"}],
[{"$hour":"$date2"}, {"$minute":"$date2"},
{"$millisecond":"$date2"}]]}}}])