The collection (Product) has two document:
[
{
"_id" : ObjectId("5af00c72cdded465976dfc41"),
"dates" : [{
"start": ISODate("2018-05-01T08:21:06.152Z")
"end": ISODate("2018-05-30T08:21:06.152Z")
}],
"minutes" : [
{
"start" : 1980, //1440*1+9*60
"end" : 8400 // 1440*5+20*60
}
]
},
{
"_id" : ObjectId("5af00c72cdded465976dfc41"),
"dates" : [],
"minutes" : [
{
"start" : 1980,
"end" : 8400
},
{
"start" : 9240,
"end" : 9720
}
]
}
]
I would like to make a criteria, that is looking for a product availiable at a specific period of time based on dates and minutes
A week is represnted in 1440 minutes, and days of week start from 0:sunday .... 6 Saturday.
My problem is when i have dates empty the document 2 is not returned, i think the problem comes from $eq. I tried $exists but it doesn't work
Document n°1 :
this product will be in store from 2018-05-01 to 2018-05-30 but only from Mondy (9am) to Friday (20pm)
Document n°2 :
1 - this product will be in store from Mondy (9am) to Friday (20pm)
{
"start" : 1980, //1440*1+9*60
"end" : 8400 // 1440*5+20*60
}
2- then in Saturday from 10 am to 18pm
{
"start" : 9240, // 1440*6+10*60
"end" : 9720 // 1440*6+18*60
}
My criteria is:
const minutesInWeek = date.days() * 1440 + date.hours() * 60 + date.minutes();
this.pModel.
find({
$and: [
{ minutes : {
$elemMatch: {
$or : [
{ minutes: {$eq: [ ]} },
{ $and: [{start: { $lte: minutesInWeek }}, {end: { $gt: minutesInWeek }} ] }
]
}
}},
{ dates : {
$elemMatch: {
$or : [
{ dates: {$eq: [ ]} },
{ $and: [{start: { $lte: new Date() }}, {end: { $gt: new Date() }} ] }
]
}
}}
]
});;
i would really appreciate some help with this, thanks!
{ $size: 0 }documentation