I am trying to build an aggregation
on MongoDB Compass Community 1.16.3 and I have a strange issue on the $match
stage querying ObjectId
and ISODate
at the same time.
The non working $match stage
{
user_id: ObjectId("5c9168ec5530c90d0c5cd98a"),
value: {$gte: 600},
datetime: { $gte: ISODate("2019-02-01T00:00:00Z"), $lt: ISODate("2019-04-10T23:59:59Z") }
}
This query does not work at all and Compass return Expected end of input but "}" found.
But these $match stages work
{
user_id: ObjectId("5c9168ec5530c90d0c5cd98a"),
value: {$gte: 600}
}
Perfect result!
{
value: {$gte: 600},
datetime: { $gte: ISODate("2019-02-01T00:00:00Z"), $lt: ISODate("2019-04-10T23:59:59Z") }
}
Perfect result!
It seems that the query does not work if I use ObjectId
and ISODate
at the same time. So, did I made a mistake somewhere? Or do I have to split it in 2 $match
stages? Any thoughts?
Edited
If I split the pipeline in 2 $match
stages (I removed value
in this example), it works well but I don't know if it is a good practice and if it is efficient!
[{
$match: {
user_id: ObjectId("5c9168ec5530c90d0c5cd98a")
}
}, {
$match: {
datetime: {
$gte: ISODate("2019-02-01T00:00:00Z"),
$lt: ISODate("2019-04-01T00:00:00Z")
}
}
}]