I have a collection situations
with documents in this structure:
{
"_id" : “32AzuAkJ8PXEgSkcuFII0YSeVUl1",
"situations" : [
{
"datetime" : "2018-11-28T21:21:49.013Z"
},
{
"datetime" : "2018-11-29T15:17:50.913Z"
},
{
"datetime" : "2018-11-30T22:50:01.684Z"
},
],
"user" : [
{
"email" : “some@email.com”,
"userType" : “new”
}
]
}
I need to filter out documents which have at least one situation between 14 days
and 7 days ago
AND at least one situation between 7 days ago
and now
.
And it has to happen through an Aggregation Pipeline, because I will need to perform other operations later.
First I tried this pipeline:
{
$match: {
$and: [
{
$expr: {
$and: [
{ "$situations.datetime": { $gt: new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000))) } },
{ "$situations.datetime": { $lte: new Date((new Date().getTime())) } }
]
}
},
{
$expr: {
$and: [
{ "$situations.datetime": { $gt: new Date((new Date().getTime() - (14 * 24 * 60 * 60 * 1000))) } },
{ "$situations.datetime": { $lte: new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000))) } }
]
}
}
]
}
}
But it didn't work. I think because my datetime
is a String.
Then I was thinking about using $dateFromString
somehow, but I didn't know how to use it within the situations
array.